effect
stringclasses 48
values | original_source_type
stringlengths 0
23k
| opens_and_abbrevs
listlengths 2
92
| isa_cross_project_example
bool 1
class | source_definition
stringlengths 9
57.9k
| partial_definition
stringlengths 7
23.3k
| is_div
bool 2
classes | is_type
null | is_proof
bool 2
classes | completed_definiton
stringlengths 1
250k
| dependencies
dict | effect_flags
sequencelengths 0
2
| ideal_premises
sequencelengths 0
236
| mutual_with
sequencelengths 0
11
| file_context
stringlengths 0
407k
| interleaved
bool 1
class | is_simply_typed
bool 2
classes | file_name
stringlengths 5
48
| vconfig
dict | is_simple_lemma
null | source_type
stringlengths 10
23k
| proof_features
sequencelengths 0
1
| name
stringlengths 8
95
| source
dict | verbose_type
stringlengths 1
7.42k
| source_range
dict |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
FStar.Pervasives.Lemma | val lemma_weaken_perm_left: #a:eqtype -> s1:seq a -> s2:seq a{length s1 = length s2} -> i:nat -> j:nat -> k:nat{i <= j /\ j <= k /\ k <= length s1}
-> Lemma
(requires (s1 == splice s2 j s1 k /\ permutation a (slice s2 j k) (slice s1 j k)))
(ensures (permutation a (slice s2 i k) (slice s1 i k))) | [
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k) | val lemma_weaken_perm_left: #a:eqtype -> s1:seq a -> s2:seq a{length s1 = length s2} -> i:nat -> j:nat -> k:nat{i <= j /\ j <= k /\ k <= length s1}
-> Lemma
(requires (s1 == splice s2 j s1 k /\ permutation a (slice s2 j k) (slice s1 j k)))
(ensures (permutation a (slice s2 i k) (slice s1 i k)))
let lemma_weaken_perm_left #_ s1 s2 i j k = | false | null | true | cut (equal (slice s2 i k) (append (slice s2 i j) (slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j) (slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k) | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Seq.Base.seq",
"Prims.b2t",
"Prims.op_Equality",
"Prims.nat",
"FStar.Seq.Base.length",
"Prims.l_and",
"Prims.op_LessThanOrEqual",
"FStar.Seq.Properties.lemma_append_count",
"FStar.Seq.Base.slice",
"Prims.unit",
"Prims.cut",
"FStar.Seq.Base.equal",
"FStar.Seq.Base.append"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j)) | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val lemma_weaken_perm_left: #a:eqtype -> s1:seq a -> s2:seq a{length s1 = length s2} -> i:nat -> j:nat -> k:nat{i <= j /\ j <= k /\ k <= length s1}
-> Lemma
(requires (s1 == splice s2 j s1 k /\ permutation a (slice s2 j k) (slice s1 j k)))
(ensures (permutation a (slice s2 i k) (slice s1 i k))) | [] | FStar.Seq.Properties.lemma_weaken_perm_left | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
s1: FStar.Seq.Base.seq a ->
s2: FStar.Seq.Base.seq a {FStar.Seq.Base.length s1 = FStar.Seq.Base.length s2} ->
i: Prims.nat ->
j: Prims.nat ->
k: Prims.nat{i <= j /\ j <= k /\ k <= FStar.Seq.Base.length s1}
-> FStar.Pervasives.Lemma
(requires
s1 == FStar.Seq.Properties.splice s2 j s1 k /\
FStar.Seq.Properties.permutation a
(FStar.Seq.Base.slice s2 j k)
(FStar.Seq.Base.slice s1 j k))
(ensures
FStar.Seq.Properties.permutation a
(FStar.Seq.Base.slice s2 i k)
(FStar.Seq.Base.slice s1 i k)) | {
"end_col": 50,
"end_line": 298,
"start_col": 2,
"start_line": 293
} |
FStar.Pervasives.Lemma | val find_l_none_no_index' (#a: Type) (s: Seq.seq a) (f: (a -> Tot bool))
: Lemma (requires (None? (find_l f s)))
(ensures (forall (i: nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s)) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f) | val find_l_none_no_index' (#a: Type) (s: Seq.seq a) (f: (a -> Tot bool))
: Lemma (requires (None? (find_l f s)))
(ensures (forall (i: nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
let rec find_l_none_no_index' (#a: Type) (s: Seq.seq a) (f: (a -> Tot bool))
: Lemma (requires (None? (find_l f s)))
(ensures (forall (i: nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s)) = | false | null | true | if Seq.length s = 0
then ()
else
(assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f) | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma",
""
] | [
"FStar.Seq.Base.seq",
"Prims.bool",
"Prims.op_Equality",
"Prims.int",
"FStar.Seq.Base.length",
"FStar.Seq.Properties.find_append_none",
"FStar.Seq.Base.create",
"FStar.Seq.Properties.head",
"FStar.Seq.Properties.tail",
"Prims.unit",
"Prims._assert",
"FStar.Seq.Base.equal",
"FStar.Seq.Properties.cons",
"FStar.Seq.Properties.find_l_none_no_index'",
"Prims.b2t",
"FStar.Pervasives.Native.uu___is_None",
"FStar.Seq.Properties.find_l",
"Prims.op_Negation",
"Prims.squash",
"Prims.l_Forall",
"Prims.nat",
"Prims.op_LessThan",
"FStar.Seq.Base.index",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i)))) | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val find_l_none_no_index' (#a: Type) (s: Seq.seq a) (f: (a -> Tot bool))
: Lemma (requires (None? (find_l f s)))
(ensures (forall (i: nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s)) | [
"recursion"
] | FStar.Seq.Properties.find_l_none_no_index' | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | s: FStar.Seq.Base.seq a -> f: (_: a -> Prims.bool)
-> FStar.Pervasives.Lemma (requires None? (FStar.Seq.Properties.find_l f s))
(ensures
forall (i: Prims.nat{i < FStar.Seq.Base.length s}).
Prims.op_Negation (f (FStar.Seq.Base.index s i)))
(decreases FStar.Seq.Base.length s) | {
"end_col": 56,
"end_line": 491,
"start_col": 2,
"start_line": 485
} |
FStar.Pervasives.Lemma | val append_cons
(#a: Type)
(c: a)
(s1 s2: seq a)
: Lemma
(ensures (append (cons c s1) s2 == cons c (append s1 s2))) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2)) | val append_cons
(#a: Type)
(c: a)
(s1 s2: seq a)
: Lemma
(ensures (append (cons c s1) s2 == cons c (append s1 s2)))
let append_cons #_ c s1 s2 = | false | null | true | lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2)) | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"FStar.Seq.Base.seq",
"FStar.Seq.Base.lemma_eq_elim",
"FStar.Seq.Base.append",
"FStar.Seq.Properties.cons",
"Prims.unit"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = () | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val append_cons
(#a: Type)
(c: a)
(s1 s2: seq a)
: Lemma
(ensures (append (cons c s1) s2 == cons c (append s1 s2))) | [] | FStar.Seq.Properties.append_cons | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | c: a -> s1: FStar.Seq.Base.seq a -> s2: FStar.Seq.Base.seq a
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.append (FStar.Seq.Properties.cons c s1) s2 ==
FStar.Seq.Properties.cons c (FStar.Seq.Base.append s1 s2)) | {
"end_col": 90,
"end_line": 511,
"start_col": 29,
"start_line": 511
} |
FStar.Pervasives.Lemma | val cons_index_slice
(#a: Type)
(s: seq a)
(i: nat)
(j: nat {i < j /\ j <= length s} )
(k:nat{k == i+1})
: Lemma
(requires True)
(ensures (cons (index s i) (slice s k j) == slice s i j))
[SMTPat (cons (index s i) (slice s k j))] | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j) | val cons_index_slice
(#a: Type)
(s: seq a)
(i: nat)
(j: nat {i < j /\ j <= length s} )
(k:nat{k == i+1})
: Lemma
(requires True)
(ensures (cons (index s i) (slice s k j) == slice s i j))
[SMTPat (cons (index s i) (slice s k j))]
let cons_index_slice #_ s i j _ = | false | null | true | lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j) | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"FStar.Seq.Base.seq",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.op_LessThanOrEqual",
"FStar.Seq.Base.length",
"Prims.eq2",
"Prims.int",
"Prims.op_Addition",
"FStar.Seq.Base.lemma_eq_elim",
"FStar.Seq.Properties.cons",
"FStar.Seq.Base.index",
"FStar.Seq.Base.slice",
"Prims.unit"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1)) | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val cons_index_slice
(#a: Type)
(s: seq a)
(i: nat)
(j: nat {i < j /\ j <= length s} )
(k:nat{k == i+1})
: Lemma
(requires True)
(ensures (cons (index s i) (slice s k j) == slice s i j))
[SMTPat (cons (index s i) (slice s k j))] | [] | FStar.Seq.Properties.cons_index_slice | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
s: FStar.Seq.Base.seq a ->
i: Prims.nat ->
j: Prims.nat{i < j /\ j <= FStar.Seq.Base.length s} ->
k: Prims.nat{k == i + 1}
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Properties.cons (FStar.Seq.Base.index s i) (FStar.Seq.Base.slice s k j) ==
FStar.Seq.Base.slice s i j)
[SMTPat (FStar.Seq.Properties.cons (FStar.Seq.Base.index s i) (FStar.Seq.Base.slice s k j))] | {
"end_col": 100,
"end_line": 519,
"start_col": 34,
"start_line": 519
} |
FStar.Pervasives.Lemma | val slice_is_empty
(#a: Type)
(s: seq a)
(i: nat {i <= length s})
: Lemma
(requires True)
(ensures (slice s i i == Seq.empty))
[SMTPat (slice s i i)] | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty | val slice_is_empty
(#a: Type)
(s: seq a)
(i: nat {i <= length s})
: Lemma
(requires True)
(ensures (slice s i i == Seq.empty))
[SMTPat (slice s i i)]
let slice_is_empty #_ s i = | false | null | true | lemma_eq_elim (slice s i i) Seq.empty | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"FStar.Seq.Base.seq",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Seq.Base.length",
"FStar.Seq.Base.lemma_eq_elim",
"FStar.Seq.Base.slice",
"FStar.Seq.Base.empty",
"Prims.unit"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j) | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val slice_is_empty
(#a: Type)
(s: seq a)
(i: nat {i <= length s})
: Lemma
(requires True)
(ensures (slice s i i == Seq.empty))
[SMTPat (slice s i i)] | [] | FStar.Seq.Properties.slice_is_empty | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | s: FStar.Seq.Base.seq a -> i: Prims.nat{i <= FStar.Seq.Base.length s}
-> FStar.Pervasives.Lemma (ensures FStar.Seq.Base.slice s i i == FStar.Seq.Base.empty)
[SMTPat (FStar.Seq.Base.slice s i i)] | {
"end_col": 65,
"end_line": 521,
"start_col": 28,
"start_line": 521
} |
FStar.Pervasives.Lemma | val intro_of_list': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s)))) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let intro_of_list' = intro_of_list'' | val intro_of_list': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s))))
let intro_of_list' = | false | null | true | intro_of_list'' | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"FStar.Seq.Properties.intro_of_list''"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2))
let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1)
let seq_of_list_tl #_ l = lemma_seq_of_list_induction l
let rec mem_seq_of_list #_ x l
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _ : squash (head (seq_of_list l) == y) = () in
let _ : squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _ : squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q
let rec intro_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s))))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd :: tl -> intro_of_list'' (i + 1) s tl | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val intro_of_list': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s)))) | [] | FStar.Seq.Properties.intro_of_list' | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | i: Prims.nat -> s: FStar.Seq.Base.seq a -> l: Prims.list a
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Base.length l + i = FStar.Seq.Base.length s /\ i <= FStar.Seq.Base.length s /\
FStar.Seq.Properties.explode_and i s l)
(ensures
FStar.Seq.Base.equal (FStar.Seq.Properties.seq_of_list l)
(FStar.Seq.Base.slice s i (FStar.Seq.Base.length s))) | {
"end_col": 36,
"end_line": 566,
"start_col": 21,
"start_line": 566
} |
FStar.Pervasives.Lemma | val slice_length
(#a: Type)
(s: seq a)
: Lemma
(requires True)
(ensures (slice s 0 (length s) == s))
[SMTPat (slice s 0 (length s))] | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s | val slice_length
(#a: Type)
(s: seq a)
: Lemma
(requires True)
(ensures (slice s 0 (length s) == s))
[SMTPat (slice s 0 (length s))]
let slice_length #_ s = | false | null | true | lemma_eq_elim (slice s 0 (length s)) s | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"FStar.Seq.Base.seq",
"FStar.Seq.Base.lemma_eq_elim",
"FStar.Seq.Base.slice",
"FStar.Seq.Base.length",
"Prims.unit"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val slice_length
(#a: Type)
(s: seq a)
: Lemma
(requires True)
(ensures (slice s 0 (length s) == s))
[SMTPat (slice s 0 (length s))] | [] | FStar.Seq.Properties.slice_length | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | s: FStar.Seq.Base.seq a
-> FStar.Pervasives.Lemma (ensures FStar.Seq.Base.slice s 0 (FStar.Seq.Base.length s) == s)
[SMTPat (FStar.Seq.Base.slice s 0 (FStar.Seq.Base.length s))] | {
"end_col": 62,
"end_line": 523,
"start_col": 24,
"start_line": 523
} |
FStar.Pervasives.Lemma | val slice_slice
(#a: Type)
(s: seq a)
(i1: nat)
(j1: nat {i1 <= j1 /\ j1 <= length s} )
(i2: nat)
(j2: nat {i2 <= j2 /\ j2 <= j1 - i1} )
: Lemma
(requires True)
(ensures (slice (slice s i1 j1) i2 j2 == slice s (i1 + i2) (i1 + j2)))
[SMTPat (slice (slice s i1 j1) i2 j2)] | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2)) | val slice_slice
(#a: Type)
(s: seq a)
(i1: nat)
(j1: nat {i1 <= j1 /\ j1 <= length s} )
(i2: nat)
(j2: nat {i2 <= j2 /\ j2 <= j1 - i1} )
: Lemma
(requires True)
(ensures (slice (slice s i1 j1) i2 j2 == slice s (i1 + i2) (i1 + j2)))
[SMTPat (slice (slice s i1 j1) i2 j2)]
let slice_slice #_ s i1 j1 i2 j2 = | false | null | true | lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2)) | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"FStar.Seq.Base.seq",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Seq.Base.length",
"Prims.op_Subtraction",
"FStar.Seq.Base.lemma_eq_elim",
"FStar.Seq.Base.slice",
"Prims.op_Addition",
"Prims.unit"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val slice_slice
(#a: Type)
(s: seq a)
(i1: nat)
(j1: nat {i1 <= j1 /\ j1 <= length s} )
(i2: nat)
(j2: nat {i2 <= j2 /\ j2 <= j1 - i1} )
: Lemma
(requires True)
(ensures (slice (slice s i1 j1) i2 j2 == slice s (i1 + i2) (i1 + j2)))
[SMTPat (slice (slice s i1 j1) i2 j2)] | [] | FStar.Seq.Properties.slice_slice | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
s: FStar.Seq.Base.seq a ->
i1: Prims.nat ->
j1: Prims.nat{i1 <= j1 /\ j1 <= FStar.Seq.Base.length s} ->
i2: Prims.nat ->
j2: Prims.nat{i2 <= j2 /\ j2 <= j1 - i1}
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.slice (FStar.Seq.Base.slice s i1 j1) i2 j2 ==
FStar.Seq.Base.slice s (i1 + i2) (i1 + j2))
[SMTPat (FStar.Seq.Base.slice (FStar.Seq.Base.slice s i1 j1) i2 j2)] | {
"end_col": 108,
"end_line": 525,
"start_col": 35,
"start_line": 525
} |
FStar.Pervasives.Lemma | val mem_seq_of_list
(#a: eqtype)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (mem x (seq_of_list l) == List.Tot.mem x l))
[SMTPat (mem x (seq_of_list l))] | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let rec mem_seq_of_list #_ x l
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _ : squash (head (seq_of_list l) == y) = () in
let _ : squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _ : squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q | val mem_seq_of_list
(#a: eqtype)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (mem x (seq_of_list l) == List.Tot.mem x l))
[SMTPat (mem x (seq_of_list l))]
let rec mem_seq_of_list #_ x l = | false | null | true | lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _:squash (head (seq_of_list l) == y) = () in
let _:squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _:squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"Prims.list",
"FStar.Seq.Properties.mem_seq_of_list",
"Prims.squash",
"Prims.eq2",
"Prims.bool",
"FStar.Seq.Properties.mem",
"FStar.Seq.Properties.seq_of_list",
"Prims.op_BarBar",
"Prims.op_Equality",
"FStar.Seq.Properties.lemma_mem_inversion",
"FStar.Seq.Base.seq",
"FStar.Seq.Properties.tail",
"FStar.Seq.Properties.seq_of_list_tl",
"FStar.Seq.Properties.head",
"Prims.unit",
"FStar.Seq.Properties.lemma_seq_of_list_induction"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2))
let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1)
let seq_of_list_tl #_ l = lemma_seq_of_list_induction l | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val mem_seq_of_list
(#a: eqtype)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (mem x (seq_of_list l) == List.Tot.mem x l))
[SMTPat (mem x (seq_of_list l))] | [
"recursion"
] | FStar.Seq.Properties.mem_seq_of_list | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: a -> l: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Properties.mem x (FStar.Seq.Properties.seq_of_list l) ==
FStar.List.Tot.Base.mem x l)
[SMTPat (FStar.Seq.Properties.mem x (FStar.Seq.Properties.seq_of_list l))] | {
"end_col": 23,
"end_line": 545,
"start_col": 2,
"start_line": 536
} |
FStar.Pervasives.Lemma | val lemma_seq_of_list_index (#a:Type) (l:list a) (i:nat{i < List.Tot.length l})
:Lemma (requires True)
(ensures (index (seq_of_list l) i == List.Tot.index l i))
[SMTPat (index (seq_of_list l) i)] | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1) | val lemma_seq_of_list_index (#a:Type) (l:list a) (i:nat{i < List.Tot.length l})
:Lemma (requires True)
(ensures (index (seq_of_list l) i == List.Tot.index l i))
[SMTPat (index (seq_of_list l) i)]
let rec lemma_seq_of_list_index #_ l i = | false | null | true | lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd :: tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1) | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"Prims.list",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.List.Tot.Base.length",
"Prims.op_Equality",
"Prims.int",
"Prims.bool",
"FStar.Seq.Properties.lemma_seq_of_list_index",
"Prims.op_Subtraction",
"Prims.unit",
"FStar.Seq.Properties.lemma_seq_of_list_induction"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2)) | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val lemma_seq_of_list_index (#a:Type) (l:list a) (i:nat{i < List.Tot.length l})
:Lemma (requires True)
(ensures (index (seq_of_list l) i == List.Tot.index l i))
[SMTPat (index (seq_of_list l) i)] | [
"recursion"
] | FStar.Seq.Properties.lemma_seq_of_list_index | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | l: Prims.list a -> i: Prims.nat{i < FStar.List.Tot.Base.length l}
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.index (FStar.Seq.Properties.seq_of_list l) i == FStar.List.Tot.Base.index l i
) [SMTPat (FStar.Seq.Base.index (FStar.Seq.Properties.seq_of_list l) i)] | {
"end_col": 72,
"end_line": 531,
"start_col": 4,
"start_line": 528
} |
FStar.Pervasives.Lemma | val elim_of_list': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l)) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let elim_of_list' = elim_of_list'' | val elim_of_list': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l))
let elim_of_list' = | false | null | true | elim_of_list'' | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"FStar.Seq.Properties.elim_of_list''"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2))
let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1)
let seq_of_list_tl #_ l = lemma_seq_of_list_induction l
let rec mem_seq_of_list #_ x l
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _ : squash (head (seq_of_list l) == y) = () in
let _ : squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _ : squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q
let rec intro_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s))))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd :: tl -> intro_of_list'' (i + 1) s tl
let intro_of_list' = intro_of_list''
let intro_of_list #_ s l = intro_of_list' 0 s l
let rec elim_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
match l with
| [] -> ()
| hd :: tl ->
lemma_seq_of_list_induction l;
elim_of_list'' (i + 1) s tl | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val elim_of_list': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l)) | [] | FStar.Seq.Properties.elim_of_list' | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | i: Prims.nat -> s: FStar.Seq.Base.seq a -> l: Prims.list a
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Base.length l + i = FStar.Seq.Base.length s /\ i <= FStar.Seq.Base.length s /\
FStar.Seq.Base.slice s i (FStar.Seq.Base.length s) == FStar.Seq.Properties.seq_of_list l)
(ensures FStar.Seq.Properties.explode_and i s l) | {
"end_col": 34,
"end_line": 590,
"start_col": 20,
"start_line": 590
} |
FStar.Pervasives.Lemma | val lemma_seq_sortwith_correctness (#a:eqtype) (f:a -> a -> Tot int) (s:seq a)
:Lemma (requires (total_order a (List.Tot.Base.bool_of_compare f)))
(ensures (let s' = sortWith f s in sorted (List.Tot.Base.bool_of_compare f) s' /\ permutation a s s')) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let lemma_seq_sortwith_correctness #_ f s
= let l = seq_to_list s in
let l' = List.Tot.Base.sortWith f l in
let s' = seq_of_list l' in
let cmp = List.Tot.Base.bool_of_compare f in
(* sortedness *)
List.Tot.Properties.sortWith_sorted f l; //the list returned by List.sortWith is sorted
lemma_seq_of_list_sorted cmp l'; //seq_of_list preserves sortedness
(* permutation *)
lemma_seq_to_list_permutation s; //seq_to_list is a permutation
List.Tot.Properties.sortWith_permutation f l; //List.sortWith is a permutation
lemma_seq_of_list_permutation l' | val lemma_seq_sortwith_correctness (#a:eqtype) (f:a -> a -> Tot int) (s:seq a)
:Lemma (requires (total_order a (List.Tot.Base.bool_of_compare f)))
(ensures (let s' = sortWith f s in sorted (List.Tot.Base.bool_of_compare f) s' /\ permutation a s s'))
let lemma_seq_sortwith_correctness #_ f s = | false | null | true | let l = seq_to_list s in
let l' = List.Tot.Base.sortWith f l in
let s' = seq_of_list l' in
let cmp = List.Tot.Base.bool_of_compare f in
List.Tot.Properties.sortWith_sorted f l;
lemma_seq_of_list_sorted cmp l';
lemma_seq_to_list_permutation s;
List.Tot.Properties.sortWith_permutation f l;
lemma_seq_of_list_permutation l' | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"Prims.int",
"FStar.Seq.Base.seq",
"FStar.Seq.Properties.lemma_seq_of_list_permutation",
"Prims.unit",
"FStar.List.Tot.Properties.sortWith_permutation",
"FStar.Seq.Properties.lemma_seq_to_list_permutation",
"FStar.Seq.Properties.lemma_seq_of_list_sorted",
"FStar.List.Tot.Properties.sortWith_sorted",
"Prims.bool",
"FStar.List.Tot.Base.bool_of_compare",
"Prims.b2t",
"Prims.op_Equality",
"Prims.nat",
"FStar.List.Tot.Base.length",
"FStar.Seq.Base.length",
"FStar.Seq.Properties.seq_of_list",
"Prims.list",
"FStar.List.Tot.Base.sortWith",
"FStar.Seq.Properties.seq_to_list"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2))
let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1)
let seq_of_list_tl #_ l = lemma_seq_of_list_induction l
let rec mem_seq_of_list #_ x l
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _ : squash (head (seq_of_list l) == y) = () in
let _ : squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _ : squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q
let rec intro_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s))))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd :: tl -> intro_of_list'' (i + 1) s tl
let intro_of_list' = intro_of_list''
let intro_of_list #_ s l = intro_of_list' 0 s l
let rec elim_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
match l with
| [] -> ()
| hd :: tl ->
lemma_seq_of_list_induction l;
elim_of_list'' (i + 1) s tl
let elim_of_list' = elim_of_list''
let elim_of_list #_ l = elim_of_list' 0 (seq_of_list l) l
let rec lemma_seq_to_list_permutation' (#a:eqtype) (s:seq a)
:Lemma (requires True) (ensures (forall x. count x s == List.Tot.Base.count x (seq_to_list s))) (decreases (length s))
= if length s > 0 then lemma_seq_to_list_permutation' (slice s 1 (length s))
let lemma_seq_to_list_permutation = lemma_seq_to_list_permutation'
let rec lemma_seq_of_list_permutation #a l
=
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| _::tl -> lemma_seq_of_list_permutation tl
let rec lemma_seq_of_list_sorted #a f l
=
lemma_seq_of_list_induction l;
if List.Tot.length l > 1 then begin
lemma_seq_of_list_induction (List.Tot.Base.tl l);
lemma_seq_of_list_sorted f (List.Tot.Base.tl l)
end | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val lemma_seq_sortwith_correctness (#a:eqtype) (f:a -> a -> Tot int) (s:seq a)
:Lemma (requires (total_order a (List.Tot.Base.bool_of_compare f)))
(ensures (let s' = sortWith f s in sorted (List.Tot.Base.bool_of_compare f) s' /\ permutation a s s')) | [] | FStar.Seq.Properties.lemma_seq_sortwith_correctness | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | f: (_: a -> _: a -> Prims.int) -> s: FStar.Seq.Base.seq a
-> FStar.Pervasives.Lemma
(requires FStar.Seq.Properties.total_order a (FStar.List.Tot.Base.bool_of_compare f))
(ensures
(let s' = FStar.Seq.Properties.sortWith f s in
FStar.Seq.Properties.sorted (FStar.List.Tot.Base.bool_of_compare f) s' /\
FStar.Seq.Properties.permutation a s s')) | {
"end_col": 36,
"end_line": 629,
"start_col": 3,
"start_line": 617
} |
FStar.Pervasives.Lemma | val intro_of_list (#a: Type) (s: seq a) (l: list a):
Lemma
(requires (
List.Tot.length l = length s /\
pointwise_and s l))
(ensures (
s == seq_of_list l)) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let intro_of_list #_ s l = intro_of_list' 0 s l | val intro_of_list (#a: Type) (s: seq a) (l: list a):
Lemma
(requires (
List.Tot.length l = length s /\
pointwise_and s l))
(ensures (
s == seq_of_list l))
let intro_of_list #_ s l = | false | null | true | intro_of_list' 0 s l | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"FStar.Seq.Base.seq",
"Prims.list",
"FStar.Seq.Properties.intro_of_list'",
"Prims.unit"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2))
let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1)
let seq_of_list_tl #_ l = lemma_seq_of_list_induction l
let rec mem_seq_of_list #_ x l
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _ : squash (head (seq_of_list l) == y) = () in
let _ : squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _ : squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q
let rec intro_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s))))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd :: tl -> intro_of_list'' (i + 1) s tl
let intro_of_list' = intro_of_list'' | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val intro_of_list (#a: Type) (s: seq a) (l: list a):
Lemma
(requires (
List.Tot.length l = length s /\
pointwise_and s l))
(ensures (
s == seq_of_list l)) | [] | FStar.Seq.Properties.intro_of_list | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | s: FStar.Seq.Base.seq a -> l: Prims.list a
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Base.length l = FStar.Seq.Base.length s /\
FStar.Seq.Properties.pointwise_and s l) (ensures s == FStar.Seq.Properties.seq_of_list l) | {
"end_col": 47,
"end_line": 568,
"start_col": 27,
"start_line": 568
} |
Prims.Tot | val map_seq (#a #b:Type) (f:a -> Tot b) (s:Seq.seq a) : Tot (Seq.seq b) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let rec map_seq #a #b f s : Tot (Seq.seq b) (decreases Seq.length s) =
if Seq.length s = 0
then Seq.empty
else let hd, tl = head s, tail s in
cons (f hd) (map_seq f tl) | val map_seq (#a #b:Type) (f:a -> Tot b) (s:Seq.seq a) : Tot (Seq.seq b)
let rec map_seq #a #b f s : Tot (Seq.seq b) (decreases Seq.length s) = | false | null | false | if Seq.length s = 0
then Seq.empty
else
let hd, tl = head s, tail s in
cons (f hd) (map_seq f tl) | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"",
"total"
] | [
"FStar.Seq.Base.seq",
"Prims.op_Equality",
"Prims.int",
"FStar.Seq.Base.length",
"FStar.Seq.Base.empty",
"Prims.bool",
"Prims.precedes",
"Prims.nat",
"FStar.Seq.Properties.cons",
"FStar.Seq.Properties.map_seq",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Seq.Properties.head",
"FStar.Seq.Properties.tail"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2))
let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1)
let seq_of_list_tl #_ l = lemma_seq_of_list_induction l
let rec mem_seq_of_list #_ x l
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _ : squash (head (seq_of_list l) == y) = () in
let _ : squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _ : squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q
let rec intro_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s))))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd :: tl -> intro_of_list'' (i + 1) s tl
let intro_of_list' = intro_of_list''
let intro_of_list #_ s l = intro_of_list' 0 s l
let rec elim_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
match l with
| [] -> ()
| hd :: tl ->
lemma_seq_of_list_induction l;
elim_of_list'' (i + 1) s tl
let elim_of_list' = elim_of_list''
let elim_of_list #_ l = elim_of_list' 0 (seq_of_list l) l
let rec lemma_seq_to_list_permutation' (#a:eqtype) (s:seq a)
:Lemma (requires True) (ensures (forall x. count x s == List.Tot.Base.count x (seq_to_list s))) (decreases (length s))
= if length s > 0 then lemma_seq_to_list_permutation' (slice s 1 (length s))
let lemma_seq_to_list_permutation = lemma_seq_to_list_permutation'
let rec lemma_seq_of_list_permutation #a l
=
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| _::tl -> lemma_seq_of_list_permutation tl
let rec lemma_seq_of_list_sorted #a f l
=
lemma_seq_of_list_induction l;
if List.Tot.length l > 1 then begin
lemma_seq_of_list_induction (List.Tot.Base.tl l);
lemma_seq_of_list_sorted f (List.Tot.Base.tl l)
end
let lemma_seq_sortwith_correctness #_ f s
= let l = seq_to_list s in
let l' = List.Tot.Base.sortWith f l in
let s' = seq_of_list l' in
let cmp = List.Tot.Base.bool_of_compare f in
(* sortedness *)
List.Tot.Properties.sortWith_sorted f l; //the list returned by List.sortWith is sorted
lemma_seq_of_list_sorted cmp l'; //seq_of_list preserves sortedness
(* permutation *)
lemma_seq_to_list_permutation s; //seq_to_list is a permutation
List.Tot.Properties.sortWith_permutation f l; //List.sortWith is a permutation
lemma_seq_of_list_permutation l' //seq_of_list is a permutation
(****** Seq map ******) | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val map_seq (#a #b:Type) (f:a -> Tot b) (s:Seq.seq a) : Tot (Seq.seq b) | [
"recursion"
] | FStar.Seq.Properties.map_seq | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | f: (_: a -> b) -> s: FStar.Seq.Base.seq a -> Prims.Tot (FStar.Seq.Base.seq b) | {
"end_col": 33,
"end_line": 638,
"start_col": 2,
"start_line": 635
} |
FStar.Pervasives.Lemma | val map_seq_len (#a #b:Type) (f:a -> Tot b) (s:Seq.seq a)
: Lemma (ensures Seq.length (map_seq f s) == Seq.length s) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let rec map_seq_len #a #b f s
: Lemma (ensures Seq.length (map_seq f s) == Seq.length s) (decreases Seq.length s)
= if Seq.length s = 0
then ()
else map_seq_len f (tail s) | val map_seq_len (#a #b:Type) (f:a -> Tot b) (s:Seq.seq a)
: Lemma (ensures Seq.length (map_seq f s) == Seq.length s)
let rec map_seq_len #a #b f s
: Lemma (ensures Seq.length (map_seq f s) == Seq.length s) (decreases Seq.length s) = | false | null | true | if Seq.length s = 0 then () else map_seq_len f (tail s) | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"",
"lemma"
] | [
"FStar.Seq.Base.seq",
"Prims.op_Equality",
"Prims.int",
"FStar.Seq.Base.length",
"Prims.bool",
"FStar.Seq.Properties.map_seq_len",
"FStar.Seq.Properties.tail",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Properties.map_seq",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2))
let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1)
let seq_of_list_tl #_ l = lemma_seq_of_list_induction l
let rec mem_seq_of_list #_ x l
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _ : squash (head (seq_of_list l) == y) = () in
let _ : squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _ : squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q
let rec intro_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s))))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd :: tl -> intro_of_list'' (i + 1) s tl
let intro_of_list' = intro_of_list''
let intro_of_list #_ s l = intro_of_list' 0 s l
let rec elim_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
match l with
| [] -> ()
| hd :: tl ->
lemma_seq_of_list_induction l;
elim_of_list'' (i + 1) s tl
let elim_of_list' = elim_of_list''
let elim_of_list #_ l = elim_of_list' 0 (seq_of_list l) l
let rec lemma_seq_to_list_permutation' (#a:eqtype) (s:seq a)
:Lemma (requires True) (ensures (forall x. count x s == List.Tot.Base.count x (seq_to_list s))) (decreases (length s))
= if length s > 0 then lemma_seq_to_list_permutation' (slice s 1 (length s))
let lemma_seq_to_list_permutation = lemma_seq_to_list_permutation'
let rec lemma_seq_of_list_permutation #a l
=
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| _::tl -> lemma_seq_of_list_permutation tl
let rec lemma_seq_of_list_sorted #a f l
=
lemma_seq_of_list_induction l;
if List.Tot.length l > 1 then begin
lemma_seq_of_list_induction (List.Tot.Base.tl l);
lemma_seq_of_list_sorted f (List.Tot.Base.tl l)
end
let lemma_seq_sortwith_correctness #_ f s
= let l = seq_to_list s in
let l' = List.Tot.Base.sortWith f l in
let s' = seq_of_list l' in
let cmp = List.Tot.Base.bool_of_compare f in
(* sortedness *)
List.Tot.Properties.sortWith_sorted f l; //the list returned by List.sortWith is sorted
lemma_seq_of_list_sorted cmp l'; //seq_of_list preserves sortedness
(* permutation *)
lemma_seq_to_list_permutation s; //seq_to_list is a permutation
List.Tot.Properties.sortWith_permutation f l; //List.sortWith is a permutation
lemma_seq_of_list_permutation l' //seq_of_list is a permutation
(****** Seq map ******)
let rec map_seq #a #b f s : Tot (Seq.seq b) (decreases Seq.length s) =
if Seq.length s = 0
then Seq.empty
else let hd, tl = head s, tail s in
cons (f hd) (map_seq f tl)
let rec map_seq_len #a #b f s | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val map_seq_len (#a #b:Type) (f:a -> Tot b) (s:Seq.seq a)
: Lemma (ensures Seq.length (map_seq f s) == Seq.length s) | [
"recursion"
] | FStar.Seq.Properties.map_seq_len | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | f: (_: a -> b) -> s: FStar.Seq.Base.seq a
-> FStar.Pervasives.Lemma
(ensures FStar.Seq.Base.length (FStar.Seq.Properties.map_seq f s) == FStar.Seq.Base.length s)
(decreases FStar.Seq.Base.length s) | {
"end_col": 31,
"end_line": 644,
"start_col": 4,
"start_line": 642
} |
FStar.Pervasives.Lemma | val map_seq_append (#a #b:Type) (f:a -> Tot b) (s1 s2:Seq.seq a)
: Lemma (ensures (map_seq f (Seq.append s1 s2) ==
Seq.append (map_seq f s1) (map_seq f s2))) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let map_seq_append #a #b f s1 s2 =
map_seq_len f s1;
map_seq_len f s2;
map_seq_len f (Seq.append s1 s2);
Classical.forall_intro (map_seq_index f s1);
Classical.forall_intro (map_seq_index f s2);
Classical.forall_intro (map_seq_index f (Seq.append s1 s2));
assert (Seq.equal (map_seq f (Seq.append s1 s2))
(Seq.append (map_seq f s1) (map_seq f s2))) | val map_seq_append (#a #b:Type) (f:a -> Tot b) (s1 s2:Seq.seq a)
: Lemma (ensures (map_seq f (Seq.append s1 s2) ==
Seq.append (map_seq f s1) (map_seq f s2)))
let map_seq_append #a #b f s1 s2 = | false | null | true | map_seq_len f s1;
map_seq_len f s2;
map_seq_len f (Seq.append s1 s2);
Classical.forall_intro (map_seq_index f s1);
Classical.forall_intro (map_seq_index f s2);
Classical.forall_intro (map_seq_index f (Seq.append s1 s2));
assert (Seq.equal (map_seq f (Seq.append s1 s2)) (Seq.append (map_seq f s1) (map_seq f s2))) | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"FStar.Seq.Base.seq",
"Prims._assert",
"FStar.Seq.Base.equal",
"FStar.Seq.Properties.map_seq",
"FStar.Seq.Base.append",
"Prims.unit",
"FStar.Classical.forall_intro",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.Seq.Base.length",
"Prims.eq2",
"FStar.Seq.Base.index",
"FStar.Seq.Properties.map_seq_index",
"FStar.Seq.Properties.map_seq_len"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2))
let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1)
let seq_of_list_tl #_ l = lemma_seq_of_list_induction l
let rec mem_seq_of_list #_ x l
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _ : squash (head (seq_of_list l) == y) = () in
let _ : squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _ : squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q
let rec intro_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s))))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd :: tl -> intro_of_list'' (i + 1) s tl
let intro_of_list' = intro_of_list''
let intro_of_list #_ s l = intro_of_list' 0 s l
let rec elim_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
match l with
| [] -> ()
| hd :: tl ->
lemma_seq_of_list_induction l;
elim_of_list'' (i + 1) s tl
let elim_of_list' = elim_of_list''
let elim_of_list #_ l = elim_of_list' 0 (seq_of_list l) l
let rec lemma_seq_to_list_permutation' (#a:eqtype) (s:seq a)
:Lemma (requires True) (ensures (forall x. count x s == List.Tot.Base.count x (seq_to_list s))) (decreases (length s))
= if length s > 0 then lemma_seq_to_list_permutation' (slice s 1 (length s))
let lemma_seq_to_list_permutation = lemma_seq_to_list_permutation'
let rec lemma_seq_of_list_permutation #a l
=
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| _::tl -> lemma_seq_of_list_permutation tl
let rec lemma_seq_of_list_sorted #a f l
=
lemma_seq_of_list_induction l;
if List.Tot.length l > 1 then begin
lemma_seq_of_list_induction (List.Tot.Base.tl l);
lemma_seq_of_list_sorted f (List.Tot.Base.tl l)
end
let lemma_seq_sortwith_correctness #_ f s
= let l = seq_to_list s in
let l' = List.Tot.Base.sortWith f l in
let s' = seq_of_list l' in
let cmp = List.Tot.Base.bool_of_compare f in
(* sortedness *)
List.Tot.Properties.sortWith_sorted f l; //the list returned by List.sortWith is sorted
lemma_seq_of_list_sorted cmp l'; //seq_of_list preserves sortedness
(* permutation *)
lemma_seq_to_list_permutation s; //seq_to_list is a permutation
List.Tot.Properties.sortWith_permutation f l; //List.sortWith is a permutation
lemma_seq_of_list_permutation l' //seq_of_list is a permutation
(****** Seq map ******)
let rec map_seq #a #b f s : Tot (Seq.seq b) (decreases Seq.length s) =
if Seq.length s = 0
then Seq.empty
else let hd, tl = head s, tail s in
cons (f hd) (map_seq f tl)
let rec map_seq_len #a #b f s
: Lemma (ensures Seq.length (map_seq f s) == Seq.length s) (decreases Seq.length s)
= if Seq.length s = 0
then ()
else map_seq_len f (tail s)
let rec map_seq_index #a #b f s i
: Lemma (ensures (map_seq_len f s; Seq.index (map_seq f s) i == f (Seq.index s i))) (decreases Seq.length s)
= map_seq_len f s;
if Seq.length s = 0
then ()
else if i = 0
then ()
else map_seq_index f (tail s) (i-1) | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val map_seq_append (#a #b:Type) (f:a -> Tot b) (s1 s2:Seq.seq a)
: Lemma (ensures (map_seq f (Seq.append s1 s2) ==
Seq.append (map_seq f s1) (map_seq f s2))) | [] | FStar.Seq.Properties.map_seq_append | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | f: (_: a -> b) -> s1: FStar.Seq.Base.seq a -> s2: FStar.Seq.Base.seq a
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Properties.map_seq f (FStar.Seq.Base.append s1 s2) ==
FStar.Seq.Base.append (FStar.Seq.Properties.map_seq f s1) (FStar.Seq.Properties.map_seq f s2)) | {
"end_col": 63,
"end_line": 663,
"start_col": 2,
"start_line": 656
} |
FStar.Pervasives.Lemma | val map_seq_index (#a #b:Type) (f:a -> Tot b) (s:Seq.seq a) (i:nat{i < Seq.length s})
: Lemma (ensures (map_seq_len f s; Seq.index (map_seq f s) i == f (Seq.index s i))) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let rec map_seq_index #a #b f s i
: Lemma (ensures (map_seq_len f s; Seq.index (map_seq f s) i == f (Seq.index s i))) (decreases Seq.length s)
= map_seq_len f s;
if Seq.length s = 0
then ()
else if i = 0
then ()
else map_seq_index f (tail s) (i-1) | val map_seq_index (#a #b:Type) (f:a -> Tot b) (s:Seq.seq a) (i:nat{i < Seq.length s})
: Lemma (ensures (map_seq_len f s; Seq.index (map_seq f s) i == f (Seq.index s i)))
let rec map_seq_index #a #b f s i
: Lemma
(ensures
(map_seq_len f s;
Seq.index (map_seq f s) i == f (Seq.index s i))) (decreases Seq.length s) = | false | null | true | map_seq_len f s;
if Seq.length s = 0 then () else if i = 0 then () else map_seq_index f (tail s) (i - 1) | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"",
"lemma"
] | [
"FStar.Seq.Base.seq",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.Seq.Base.length",
"Prims.op_Equality",
"Prims.int",
"Prims.bool",
"FStar.Seq.Properties.map_seq_index",
"FStar.Seq.Properties.tail",
"Prims.op_Subtraction",
"Prims.unit",
"FStar.Seq.Properties.map_seq_len",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.Seq.Base.index",
"FStar.Seq.Properties.map_seq",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2))
let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1)
let seq_of_list_tl #_ l = lemma_seq_of_list_induction l
let rec mem_seq_of_list #_ x l
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _ : squash (head (seq_of_list l) == y) = () in
let _ : squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _ : squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q
let rec intro_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s))))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd :: tl -> intro_of_list'' (i + 1) s tl
let intro_of_list' = intro_of_list''
let intro_of_list #_ s l = intro_of_list' 0 s l
let rec elim_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
match l with
| [] -> ()
| hd :: tl ->
lemma_seq_of_list_induction l;
elim_of_list'' (i + 1) s tl
let elim_of_list' = elim_of_list''
let elim_of_list #_ l = elim_of_list' 0 (seq_of_list l) l
let rec lemma_seq_to_list_permutation' (#a:eqtype) (s:seq a)
:Lemma (requires True) (ensures (forall x. count x s == List.Tot.Base.count x (seq_to_list s))) (decreases (length s))
= if length s > 0 then lemma_seq_to_list_permutation' (slice s 1 (length s))
let lemma_seq_to_list_permutation = lemma_seq_to_list_permutation'
let rec lemma_seq_of_list_permutation #a l
=
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| _::tl -> lemma_seq_of_list_permutation tl
let rec lemma_seq_of_list_sorted #a f l
=
lemma_seq_of_list_induction l;
if List.Tot.length l > 1 then begin
lemma_seq_of_list_induction (List.Tot.Base.tl l);
lemma_seq_of_list_sorted f (List.Tot.Base.tl l)
end
let lemma_seq_sortwith_correctness #_ f s
= let l = seq_to_list s in
let l' = List.Tot.Base.sortWith f l in
let s' = seq_of_list l' in
let cmp = List.Tot.Base.bool_of_compare f in
(* sortedness *)
List.Tot.Properties.sortWith_sorted f l; //the list returned by List.sortWith is sorted
lemma_seq_of_list_sorted cmp l'; //seq_of_list preserves sortedness
(* permutation *)
lemma_seq_to_list_permutation s; //seq_to_list is a permutation
List.Tot.Properties.sortWith_permutation f l; //List.sortWith is a permutation
lemma_seq_of_list_permutation l' //seq_of_list is a permutation
(****** Seq map ******)
let rec map_seq #a #b f s : Tot (Seq.seq b) (decreases Seq.length s) =
if Seq.length s = 0
then Seq.empty
else let hd, tl = head s, tail s in
cons (f hd) (map_seq f tl)
let rec map_seq_len #a #b f s
: Lemma (ensures Seq.length (map_seq f s) == Seq.length s) (decreases Seq.length s)
= if Seq.length s = 0
then ()
else map_seq_len f (tail s)
let rec map_seq_index #a #b f s i | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val map_seq_index (#a #b:Type) (f:a -> Tot b) (s:Seq.seq a) (i:nat{i < Seq.length s})
: Lemma (ensures (map_seq_len f s; Seq.index (map_seq f s) i == f (Seq.index s i))) | [
"recursion"
] | FStar.Seq.Properties.map_seq_index | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | f: (_: a -> b) -> s: FStar.Seq.Base.seq a -> i: Prims.nat{i < FStar.Seq.Base.length s}
-> FStar.Pervasives.Lemma
(ensures
([@@ FStar.Pervasives.inline_let ]let _ = FStar.Seq.Properties.map_seq_len f s in
FStar.Seq.Base.index (FStar.Seq.Properties.map_seq f s) i == f (FStar.Seq.Base.index s i))
) (decreases FStar.Seq.Base.length s) | {
"end_col": 39,
"end_line": 653,
"start_col": 4,
"start_line": 648
} |
FStar.Pervasives.Lemma | val lemma_swap_permutes_aux: #a:eqtype -> s:seq a -> i:nat{i<length s} -> j:nat{i <= j && j<length s} -> x:a -> Lemma
(requires True)
(ensures (count x s = count x (swap s i j))) | [
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end | val lemma_swap_permutes_aux: #a:eqtype -> s:seq a -> i:nat{i<length s} -> j:nat{i <= j && j<length s} -> x:a -> Lemma
(requires True)
(ensures (count x s = count x (swap s i j)))
let lemma_swap_permutes_aux #_ s i j x = | false | null | true | if j = i
then cut (equal (swap s i j) s)
else
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4
in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4
in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma"
] | [
"Prims.eqtype",
"FStar.Seq.Base.seq",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.Seq.Base.length",
"Prims.op_AmpAmp",
"Prims.op_LessThanOrEqual",
"Prims.op_Equality",
"Prims.l_or",
"Prims.cut",
"FStar.Seq.Base.equal",
"FStar.Seq.Properties.swap",
"Prims.bool",
"FStar.Seq.Properties.lemma_append_count_aux",
"Prims.unit",
"FStar.Seq.Base.append",
"FStar.Seq.Properties.lemma_swap_permutes_aux_frag_eq",
"Prims.op_Addition",
"FStar.Pervasives.Native.tuple5",
"FStar.Pervasives.Native.Mktuple5",
"FStar.Seq.Base.index",
"FStar.Seq.Properties.split_5"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20" | false | false | FStar.Seq.Properties.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lemma_swap_permutes_aux: #a:eqtype -> s:seq a -> i:nat{i<length s} -> j:nat{i <= j && j<length s} -> x:a -> Lemma
(requires True)
(ensures (count x s = count x (swap s i j))) | [] | FStar.Seq.Properties.lemma_swap_permutes_aux | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
s: FStar.Seq.Base.seq a ->
i: Prims.nat{i < FStar.Seq.Base.length s} ->
j: Prims.nat{i <= j && j < FStar.Seq.Base.length s} ->
x: a
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Properties.count x s =
FStar.Seq.Properties.count x (FStar.Seq.Properties.swap s i j)) | {
"end_col": 5,
"end_line": 189,
"start_col": 2,
"start_line": 165
} |
FStar.Pervasives.Lemma | val elim_of_list'' (#a: Type) (i: nat) (s: seq a) (l: list a)
: Lemma
(requires
(List.Tot.length l + i = length s /\ i <= length s /\ slice s i (length s) == seq_of_list l)
) (ensures (explode_and i s l)) (decreases (List.Tot.length l)) | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq.Base",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "FStar.Seq.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"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
}
] | false | let rec elim_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
match l with
| [] -> ()
| hd :: tl ->
lemma_seq_of_list_induction l;
elim_of_list'' (i + 1) s tl | val elim_of_list'' (#a: Type) (i: nat) (s: seq a) (l: list a)
: Lemma
(requires
(List.Tot.length l + i = length s /\ i <= length s /\ slice s i (length s) == seq_of_list l)
) (ensures (explode_and i s l)) (decreases (List.Tot.length l))
let rec elim_of_list'': #a: Type -> i: nat -> s: seq a -> l: list a
-> Lemma
(requires
(List.Tot.length l + i = length s /\ i <= length s /\ slice s i (length s) == seq_of_list l)
) (ensures (explode_and i s l)) (decreases (List.Tot.length l)) = | false | null | true | fun #_ i s l ->
match l with
| [] -> ()
| hd :: tl ->
lemma_seq_of_list_induction l;
elim_of_list'' (i + 1) s tl | {
"checked_file": "FStar.Seq.Properties.fst.checked",
"dependencies": [
"prims.fst.checked",
"FStar.Squash.fsti.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "FStar.Seq.Properties.fst"
} | [
"lemma",
""
] | [
"Prims.nat",
"FStar.Seq.Base.seq",
"Prims.list",
"FStar.Seq.Properties.elim_of_list''",
"Prims.op_Addition",
"Prims.unit",
"FStar.Seq.Properties.lemma_seq_of_list_induction"
] | [] | (*
Copyright 2008-2014 Nikhil Swamy and 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.Seq.Properties
open FStar.Seq.Base
module Seq = FStar.Seq.Base
let lemma_append_inj_l #_ s1 s2 t1 t2 i =
assert (index s1 i == (index (append s1 s2) i));
assert (index t1 i == (index (append t1 t2) i))
let lemma_append_inj_r #_ s1 s2 t1 t2 i =
assert (index s2 i == (index (append s1 s2) (i + length s1)));
assert (index t2 i == (index (append t1 t2) (i + length t1)))
let lemma_append_len_disj #_ s1 s2 t1 t2 =
cut (length (append s1 s2) == length s1 + length s2);
cut (length (append t1 t2) == length t1 + length t2)
let lemma_append_inj #_ s1 s2 t1 t2 =
lemma_append_len_disj s1 s2 t1 t2;
FStar.Classical.forall_intro #(i:nat{i < length s1}) #(fun i -> index s1 i == index t1 i) (lemma_append_inj_l s1 s2 t1 t2);
FStar.Classical.forall_intro #(i:nat{i < length s2}) #(fun i -> index s2 i == index t2 i) (lemma_append_inj_r s1 s2 t1 t2)
let lemma_head_append #_ _ _ = ()
let lemma_tail_append #_ s1 s2 = cut (equal (tail (append s1 s2)) (append (tail s1) s2))
let lemma_cons_inj #_ v1 v2 s1 s2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj t1 s1 t2 s2;
assert(index t1 0 == index t2 0)
let lemma_split #_ s i =
cut (equal (append (fst (split s i)) (snd (split s i))) s)
let rec mem_index' (#a:eqtype) (x:a) (s:seq a)
: Lemma (requires (mem x s))
(ensures (exists i. index s i == x))
(decreases (length s))
= if length s = 0 then ()
else if head s = x then ()
else mem_index' x (tail s)
let mem_index = mem_index'
let lemma_slice_append #_ _ _ = ()
let rec lemma_slice_first_in_append' (#a:Type) (s1:seq a) (s2:seq a)
(i:nat{i <= length s1})
: Lemma
(ensures (equal (slice (append s1 s2) i (length (append s1 s2))) (append (slice s1 i (length s1)) s2)))
(decreases (length s1))
= if i = 0 then ()
else lemma_slice_first_in_append' (tail s1) s2 (i - 1)
let lemma_slice_first_in_append = lemma_slice_first_in_append'
let slice_upd #_ s i j k v =
lemma_eq_intro (slice (upd s k v) i j) (slice s i j)
let upd_slice #_ s i j k v =
lemma_eq_intro (upd (slice s i j) k v) (slice (upd s (i + k) v) i j)
let lemma_append_cons #_ _ _ = ()
let lemma_tl #_ _ _ = ()
let rec sorted_feq' (#a:Type)
(f g : (a -> a -> Tot bool))
(s:seq a{forall x y. f x y == g x y})
: Lemma (ensures (sorted f s <==> sorted g s))
(decreases (length s))
= if length s <= 1 then ()
else sorted_feq' f g (tail s)
let sorted_feq = sorted_feq'
let rec lemma_append_count' (#a:eqtype) (lo:seq a) (hi:seq a)
: Lemma
(requires True)
(ensures (forall x. count x (append lo hi) = (count x lo + count x hi)))
(decreases (length lo))
= if length lo = 0
then cut (equal (append lo hi) hi)
else (cut (equal (cons (head lo) (append (tail lo) hi))
(append lo hi));
lemma_append_count' (tail lo) hi;
let tl_l_h = append (tail lo) hi in
let lh = cons (head lo) tl_l_h in
cut (equal (tail lh) tl_l_h))
let lemma_append_count = lemma_append_count'
let lemma_append_count_aux #_ _ lo hi = lemma_append_count lo hi
let lemma_mem_inversion #_ _ = ()
let rec lemma_mem_count' (#a:eqtype) (s:seq a) (f:a -> Tot bool)
: Lemma
(requires (forall (i:nat{i<length s}). f (index s i)))
(ensures (forall (x:a). mem x s ==> f x))
(decreases (length s))
= if length s = 0
then ()
else (let t = i:nat{i<length (tail s)} in
cut (forall (i:t). index (tail s) i = index s (i + 1));
lemma_mem_count' (tail s) f)
let lemma_mem_count = lemma_mem_count'
let lemma_count_slice #_ s i =
cut (equal s (append (slice s 0 i) (slice s i (length s))));
lemma_append_count (slice s 0 i) (slice s i (length s))
let rec sorted_concat_lemma': #a:eqtype
-> f:(a -> a -> Tot bool){total_order a f}
-> lo:seq a{sorted f lo}
-> pivot:a
-> hi:seq a{sorted f hi}
-> Lemma (requires (forall y. (mem y lo ==> f y pivot)
/\ (mem y hi ==> f pivot y)))
(ensures (sorted f (append lo (cons pivot hi))))
(decreases (length lo))
= fun #_ f lo pivot hi ->
if length lo = 0
then (cut (equal (append lo (cons pivot hi)) (cons pivot hi));
cut (equal (tail (cons pivot hi)) hi))
else (sorted_concat_lemma' f (tail lo) pivot hi;
lemma_append_cons lo (cons pivot hi);
lemma_tl (head lo) (append (tail lo) (cons pivot hi)))
let sorted_concat_lemma = sorted_concat_lemma'
let split_5 #a s i j =
let frag_lo = slice s 0 i in
let frag_i = slice s i (i + 1) in
let frag_mid = slice s (i + 1) j in
let frag_j = slice s j (j + 1) in
let frag_hi = slice s (j + 1) (length s) in
upd (upd (upd (upd (create 5 frag_lo) 1 frag_i) 2 frag_mid) 3 frag_j) 4 frag_hi
let lemma_swap_permutes_aux_frag_eq #a s i j i' j' =
cut (equal (slice s i' j') (slice (swap s i j) i' j'));
cut (equal (slice s i (i + 1)) (slice (swap s i j) j (j + 1)));
cut (equal (slice s j (j + 1)) (slice (swap s i j) i (i + 1)))
#push-options "--z3rlimit 20"
let lemma_swap_permutes_aux #_ s i j x =
if j=i
then cut (equal (swap s i j) s)
else begin
let s5 = split_5 s i j in
let frag_lo, frag_i, frag_mid, frag_j, frag_hi =
index s5 0, index s5 1, index s5 2, index s5 3, index s5 4 in
lemma_append_count_aux x frag_lo (append frag_i (append frag_mid (append frag_j frag_hi)));
lemma_append_count_aux x frag_i (append frag_mid (append frag_j frag_hi));
lemma_append_count_aux x frag_mid (append frag_j frag_hi);
lemma_append_count_aux x frag_j frag_hi;
let s' = swap s i j in
let s5' = split_5 s' i j in
let frag_lo', frag_j', frag_mid', frag_i', frag_hi' =
index s5' 0, index s5' 1, index s5' 2, index s5' 3, index s5' 4 in
lemma_swap_permutes_aux_frag_eq s i j 0 i;
lemma_swap_permutes_aux_frag_eq s i j (i + 1) j;
lemma_swap_permutes_aux_frag_eq s i j (j + 1) (length s);
lemma_append_count_aux x frag_lo (append frag_j (append frag_mid (append frag_i frag_hi)));
lemma_append_count_aux x frag_j (append frag_mid (append frag_i frag_hi));
lemma_append_count_aux x frag_mid (append frag_i frag_hi);
lemma_append_count_aux x frag_i frag_hi
end
#pop-options
let append_permutations #a s1 s2 s1' s2' =
(
lemma_append_count s1 s2;
lemma_append_count s1' s2'
)
let lemma_swap_permutes #a s i j
= FStar.Classical.forall_intro
#a
#(fun x -> count x s = count x (swap s i j))
(lemma_swap_permutes_aux s i j)
(* perm_len:
A lemma that shows that two sequences that are permutations
of each other also have the same length
*)
//a proof optimization: Z3 only needs to unfold the recursive definition of `count` once
#push-options "--fuel 1 --ifuel 1 --z3rlimit 20"
let rec perm_len' (#a:eqtype) (s1 s2: seq a)
: Lemma (requires (permutation a s1 s2))
(ensures (length s1 == length s2))
(decreases (length s1))
= if length s1 = 0 then begin
if length s2 = 0 then ()
else assert (count (index s2 0) s2 > 0)
end
else let s1_hd = head s1 in
let s1_tl = tail s1 in
assert (count s1_hd s1 > 0);
assert (count s1_hd s2 > 0);
assert (length s2 > 0);
let s2_hd = head s2 in
let s2_tl = tail s2 in
if s1_hd = s2_hd
then (assert (permutation a s1_tl s2_tl); perm_len' s1_tl s2_tl)
else let i = index_mem s1_hd s2 in
let s_pfx, s_sfx = split_eq s2 i in
assert (equal s_sfx (append (create 1 s1_hd) (tail s_sfx)));
let s2' = append s_pfx (tail s_sfx) in
lemma_append_count s_pfx s_sfx;
lemma_append_count (create 1 s1_hd) (tail s_sfx);
lemma_append_count s_pfx (tail s_sfx);
assert (permutation a s1_tl s2');
perm_len' s1_tl s2'
#pop-options
let perm_len = perm_len'
let cons_perm #_ tl s = lemma_tl (head s) tl
let lemma_mem_append #_ s1 s2 = lemma_append_count s1 s2
let lemma_slice_cons #_ s i j =
cut (equal (slice s i j) (append (create 1 (index s i)) (slice s (i + 1) j)));
lemma_mem_append (create 1 (index s i)) (slice s (i + 1) j)
let lemma_slice_snoc #_ s i j =
cut (equal (slice s i j) (append (slice s i (j - 1)) (create 1 (index s (j - 1)))));
lemma_mem_append (slice s i (j - 1)) (create 1 (index s (j - 1)))
let lemma_ordering_lo_snoc #_ f s i j pv =
cut (equal (slice s i (j + 1)) (append (slice s i j) (create 1 (index s j))));
lemma_mem_append (slice s i j) (create 1 (index s j))
let lemma_ordering_hi_cons #_ f s back len pv =
cut (equal (slice s back len) (append (create 1 (index s back)) (slice s (back + 1) len)));
lemma_mem_append (create 1 (index s back)) (slice s (back + 1) len)
let swap_frame_lo #_ s lo i j = cut (equal (slice s lo i) (slice (swap s i j) lo i))
let swap_frame_lo' #_ s lo i' i j = cut (equal (slice s lo i') (slice (swap s i j) lo i'))
let swap_frame_hi #_ s i j k hi = cut (equal (slice s k hi) (slice (swap s i j) k hi))
let lemma_swap_slice_commute #_ s start i j len =
cut (equal (slice (swap s i j) start len) (swap (slice s start len) (i - start) (j - start)))
let lemma_swap_permutes_slice #_ s start i j len =
lemma_swap_slice_commute s start i j len;
lemma_swap_permutes (slice s start len) (i - start) (j - start)
let splice_refl #_ s i j = cut (equal s (splice s i s j))
let lemma_swap_splice #_ s start i j len = cut (equal (swap s i j) (splice s start (swap s i j) len))
let lemma_seq_frame_hi #_ s1 s2 i j m n =
cut (equal (slice s1 m n) (slice s2 m n))
let lemma_seq_frame_lo #_ s1 s2 i j m n =
cut (equal (slice s1 i j) (slice s2 i j))
let lemma_tail_slice #_ s i j =
cut (equal (tail (slice s i j)) (slice s (i + 1) j))
let lemma_weaken_frame_right #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_weaken_frame_left #_ s1 s2 i j k = cut (equal s1 (splice s2 i s1 k))
let lemma_trans_frame #_ s1 s2 s3 i j = cut (equal s1 (splice s3 i s1 j))
let lemma_weaken_perm_left #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s2 i j)
(slice s1 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s2 i j) (slice s1 j k)
let lemma_weaken_perm_right #_ s1 s2 i j k =
cut (equal (slice s2 i k) (append (slice s2 i j)
(slice s2 j k)));
cut (equal (slice s1 i k) (append (slice s1 i j)
(slice s2 j k)));
lemma_append_count (slice s2 i j) (slice s2 j k);
lemma_append_count (slice s1 i j) (slice s2 j k)
let lemma_trans_perm #_ _ _ _ _ _ = ()
let lemma_cons_snoc #_ _ _ _ = ()
let lemma_tail_snoc #_ s x = lemma_slice_first_in_append s (Seq.create 1 x) 1
let lemma_snoc_inj #_ s1 s2 v1 v2 =
let t1 = create 1 v1 in
let t2 = create 1 v2 in
lemma_append_inj s1 t1 s2 t2;
assert(head t1 == head t2)
let lemma_mem_snoc #_ s x = lemma_append_count s (Seq.create 1 x)
let rec find_append_some': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (Some? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if f (head s1) then ()
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_some' (tail s1) s2 f
let find_append_some = find_append_some'
let rec find_append_none': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s1)))
(ensures (find_l f (append s1 s2) == find_l f s2))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else
let _ = cut (equal (tail (append s1 s2)) (append (tail s1) s2)) in
find_append_none' (tail s1) s2 f
let find_append_none = find_append_none'
let rec find_append_none_s2': #a:Type -> s1:seq a -> s2:seq a -> f:(a -> Tot bool) -> Lemma
(requires (None? (find_l f s2)))
(ensures (find_l f (append s1 s2) == find_l f s1))
(decreases (length s1))
= fun #_ s1 s2 f ->
if Seq.length s1 = 0 then cut (equal (append s1 s2) s2)
else if f (head s1) then ()
else begin
find_append_none_s2' (tail s1) s2 f;
cut (equal (tail (append s1 s2)) (append (tail s1) s2))
end
let find_append_none_s2 = find_append_none_s2'
let find_snoc #_ s x f =
if Some? (find_l f s) then find_append_some s (Seq.create 1 x) f
else find_append_none s (Seq.create 1 x) f
let un_snoc_snoc #_ s x =
let s', x = un_snoc (snoc s x) in
assert (Seq.equal s s')
let find_mem #_ s f x
= match seq_find f s with
| None -> mem_index x s
| Some _ -> ()
let rec seq_mem_k': #a:eqtype -> s:seq a -> n:nat{n < Seq.length s} ->
Lemma (requires True)
(ensures (mem (Seq.index s n) s))
(decreases n)
= fun #_ s n ->
if n = 0 then ()
else let tl = tail s in
seq_mem_k' tl (n - 1)
let seq_mem_k = seq_mem_k'
module L = FStar.List.Tot
let lemma_seq_of_list_induction #_ l
= match l with
| [] -> assert_norm (seq_of_list l == Seq.empty)
| hd::tl ->
assert_norm (seq_of_list (hd::tl) == create 1 hd @| seq_of_list tl);
lemma_tl hd (seq_of_list tl)
let rec lemma_seq_list_bij': #a:Type -> s:seq a -> Lemma
(requires (True))
(ensures (seq_of_list (seq_to_list s) == s))
(decreases (length s))
= fun #_ s ->
let l = seq_to_list s in
lemma_seq_of_list_induction l;
if length s = 0 then (
assert (equal s (seq_of_list l))
)
else (
lemma_seq_list_bij' (slice s 1 (length s));
assert (equal s (seq_of_list (seq_to_list s)))
)
let lemma_seq_list_bij = lemma_seq_list_bij'
let rec lemma_list_seq_bij': #a:Type -> l:list a -> Lemma
(requires (True))
(ensures (seq_to_list (seq_of_list l) == l))
(decreases (L.length l))
= fun #_ l ->
lemma_seq_of_list_induction l;
if L.length l = 0 then ()
else (
lemma_list_seq_bij' (L.tl l);
assert(equal (seq_of_list (L.tl l)) (slice (seq_of_list l) 1 (length (seq_of_list l))))
)
let lemma_list_seq_bij = lemma_list_seq_bij'
let rec lemma_index_is_nth': #a:Type -> s:seq a -> i:nat{i < length s} -> Lemma
(requires True)
(ensures (L.index (seq_to_list s) i == index s i))
(decreases i)
= fun #_ s i ->
if i = 0 then ()
else (
lemma_index_is_nth' (slice s 1 (length s)) (i-1)
)
let lemma_index_is_nth = lemma_index_is_nth'
let contains #a s x =
exists (k:nat). k < Seq.length s /\ Seq.index s k == x
let contains_intro #_ _ _ _ = ()
let contains_elim #_ _ _ = ()
let lemma_contains_empty #_ = ()
let lemma_contains_singleton #_ _ = ()
private let intro_append_contains_from_disjunction (#a:Type) (s1:seq a) (s2:seq a) (x:a)
: Lemma (requires s1 `contains` x \/ s2 `contains` x)
(ensures (append s1 s2) `contains` x)
= let open FStar.Classical in
let open FStar.Squash in
or_elim #(s1 `contains` x) #(s2 `contains` x) #(fun _ -> (append s1 s2) `contains` x)
(fun _ -> ())
(fun _ -> let s = append s1 s2 in
exists_elim (s `contains` x) (get_proof (s2 `contains` x)) (fun k ->
assert (Seq.index s (Seq.length s1 + k) == x)))
let append_contains_equiv #_ s1 s2 x
= FStar.Classical.move_requires (intro_append_contains_from_disjunction s1 s2) x
let contains_snoc #_ s x =
FStar.Classical.forall_intro (append_contains_equiv s (Seq.create 1 x))
let rec lemma_find_l_contains' (#a:Type) (f:a -> Tot bool) (l:seq a)
: Lemma (requires True) (ensures Some? (find_l f l) ==> l `contains` (Some?.v (find_l f l)))
(decreases (Seq.length l))
= if length l = 0 then ()
else if f (head l) then ()
else lemma_find_l_contains' f (tail l)
let lemma_find_l_contains = lemma_find_l_contains'
let contains_cons #_ hd tl x
= append_contains_equiv (Seq.create 1 hd) tl x
let append_cons_snoc #_ _ _ _ = ()
let append_slices #_ _ _ = ()
let rec find_l_none_no_index' (#a:Type) (s:Seq.seq a) (f:(a -> Tot bool)) :
Lemma (requires (None? (find_l f s)))
(ensures (forall (i:nat{i < Seq.length s}). not (f (Seq.index s i))))
(decreases (Seq.length s))
= if Seq.length s = 0
then ()
else (assert (not (f (head s)));
assert (None? (find_l f (tail s)));
find_l_none_no_index' (tail s) f;
assert (Seq.equal s (cons (head s) (tail s)));
find_append_none (create 1 (head s)) (tail s) f)
let find_l_none_no_index = find_l_none_no_index'
let cons_head_tail #_ s =
let _ : squash (slice s 0 1 == create 1 (index s 0)) =
lemma_index_slice s 0 1 0;
lemma_index_create 1 (index s 0) 0;
lemma_eq_elim (slice s 0 1) (create 1 (index s 0))
in
lemma_split s 1
let head_cons #_ _ _ = ()
let suffix_of_tail #_ s = cons_head_tail s
let index_cons_l #_ _ _ = ()
let index_cons_r #_ _ _ _ = ()
let append_cons #_ c s1 s2 = lemma_eq_elim (append (cons c s1) s2) (cons c (append s1 s2))
let index_tail #_ _ _ = ()
let mem_cons #_ x s = lemma_append_count (create 1 x) s
let snoc_slice_index #_ s i j = lemma_eq_elim (snoc (slice s i j) (index s j)) (slice s i (j + 1))
let cons_index_slice #_ s i j _ = lemma_eq_elim (cons (index s i) (slice s (i + 1) j)) (slice s i j)
let slice_is_empty #_ s i = lemma_eq_elim (slice s i i) Seq.empty
let slice_length #_ s = lemma_eq_elim (slice s 0 (length s)) s
let slice_slice #_ s i1 j1 i2 j2 = lemma_eq_elim (slice (slice s i1 j1) i2 j2) (slice s (i1 + i2) (i1 + j2))
let rec lemma_seq_of_list_index #_ l i
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd::tl -> if i = 0 then () else lemma_seq_of_list_index tl (i - 1)
let seq_of_list_tl #_ l = lemma_seq_of_list_induction l
let rec mem_seq_of_list #_ x l
= lemma_seq_of_list_induction l;
match l with
| [] -> ()
| y :: q ->
let _ : squash (head (seq_of_list l) == y) = () in
let _ : squash (tail (seq_of_list l) == seq_of_list q) = seq_of_list_tl l in
let _ : squash (mem x (seq_of_list l) == (x = y || mem x (seq_of_list q))) =
lemma_mem_inversion (seq_of_list l)
in
mem_seq_of_list x q
let rec intro_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
explode_and i s l))
(ensures (
equal (seq_of_list l) (slice s i (length s))))
(decreases (
List.Tot.length l))
= fun #_ i s l ->
lemma_seq_of_list_induction l;
match l with
| [] -> ()
| hd :: tl -> intro_of_list'' (i + 1) s tl
let intro_of_list' = intro_of_list''
let intro_of_list #_ s l = intro_of_list' 0 s l
let rec elim_of_list'': #a:Type ->
i:nat ->
s:seq a ->
l:list a ->
Lemma
(requires (
List.Tot.length l + i = length s /\
i <= length s /\
slice s i (length s) == seq_of_list l))
(ensures (
explode_and i s l))
(decreases ( | false | false | FStar.Seq.Properties.fst | {
"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"
} | null | val elim_of_list'' (#a: Type) (i: nat) (s: seq a) (l: list a)
: Lemma
(requires
(List.Tot.length l + i = length s /\ i <= length s /\ slice s i (length s) == seq_of_list l)
) (ensures (explode_and i s l)) (decreases (List.Tot.length l)) | [
"recursion"
] | FStar.Seq.Properties.elim_of_list'' | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | i: Prims.nat -> s: FStar.Seq.Base.seq a -> l: Prims.list a
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Base.length l + i = FStar.Seq.Base.length s /\ i <= FStar.Seq.Base.length s /\
FStar.Seq.Base.slice s i (FStar.Seq.Base.length s) == FStar.Seq.Properties.seq_of_list l)
(ensures FStar.Seq.Properties.explode_and i s l)
(decreases FStar.List.Tot.Base.length l) | {
"end_col": 33,
"end_line": 588,
"start_col": 2,
"start_line": 583
} |
FStar.Pervasives.Lemma | val downgrade_val_raise_val_u0_u1 :
#a:Type0 ->
x:a ->
Lemma
(ensures FStar.Universe.downgrade_val u#0 u#1 (FStar.Universe.raise_val x) == x)
[SMTPat (FStar.Universe.raise_val x)] | [
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": false,
"full_module": "Vale.Def.PossiblyMonad",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Print_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instruction_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Bytes_Code_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"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
}
] | false | let downgrade_val_raise_val_u0_u1 #a x = FStar.Universe.downgrade_val_raise_val #a x | val downgrade_val_raise_val_u0_u1 :
#a:Type0 ->
x:a ->
Lemma
(ensures FStar.Universe.downgrade_val u#0 u#1 (FStar.Universe.raise_val x) == x)
[SMTPat (FStar.Universe.raise_val x)]
let downgrade_val_raise_val_u0_u1 #a x = | false | null | true | FStar.Universe.downgrade_val_raise_val #a x | {
"checked_file": "Vale.Transformers.Locations.fst.checked",
"dependencies": [
"Vale.X64.Print_s.fst.checked",
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.X64.Instructions_s.fsti.checked",
"Vale.X64.Instruction_s.fsti.checked",
"Vale.X64.Bytes_Code_s.fst.checked",
"Vale.Def.PossiblyMonad.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"FStar.Universe.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Transformers.Locations.fst"
} | [
"lemma"
] | [
"FStar.Universe.downgrade_val_raise_val",
"Prims.unit"
] | [] | module Vale.Transformers.Locations
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
open Vale.X64.Bytes_Code_s
open Vale.X64.Instruction_s
open Vale.X64.Instructions_s
open Vale.X64.Machine_Semantics_s
open Vale.X64.Machine_s
open Vale.X64.Print_s
open Vale.Def.PossiblyMonad
module L = FStar.List.Tot
(* See fsti *)
type location : eqtype =
| ALocMem : location
| ALocStack: location
| ALocReg : reg -> location
| ALocCf : location
| ALocOf : location
let aux_print_reg_from_location (a:location{ALocReg? a}) : string =
let ALocReg (Reg file id) = a in
match file with
| 0 -> print_reg_name id
| 1 -> print_xmm id gcc
(* See fsti *)
let disjoint_location a1 a2 =
match a1, a2 with
| ALocCf, ALocCf -> ffalse "carry flag not disjoint from itself"
| ALocOf, ALocOf -> ffalse "overflow flag not disjoint from itself"
| ALocCf, _ | ALocOf, _ | _, ALocCf | _, ALocOf -> ttrue
| ALocMem, ALocMem -> ffalse "memory not disjoint from itself"
| ALocStack, ALocStack -> ffalse "stack not disjoint from itself"
| ALocMem, ALocStack | ALocStack, ALocMem -> ttrue
| ALocReg r1, ALocReg r2 ->
(r1 <> r2) /- ("register " ^ aux_print_reg_from_location a1 ^ " not disjoint from itself")
| ALocReg _, _ | _, ALocReg _ -> ttrue
(* See fsti *)
let auto_lemma_disjoint_location a1 a2 = () | false | false | Vale.Transformers.Locations.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val downgrade_val_raise_val_u0_u1 :
#a:Type0 ->
x:a ->
Lemma
(ensures FStar.Universe.downgrade_val u#0 u#1 (FStar.Universe.raise_val x) == x)
[SMTPat (FStar.Universe.raise_val x)] | [] | Vale.Transformers.Locations.downgrade_val_raise_val_u0_u1 | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | x: a
-> FStar.Pervasives.Lemma (ensures FStar.Universe.downgrade_val (FStar.Universe.raise_val x) == x)
[SMTPat (FStar.Universe.raise_val x)] | {
"end_col": 84,
"end_line": 47,
"start_col": 41,
"start_line": 47
} |
Prims.Tot | val aux_print_reg_from_location (a: location{ALocReg? a}) : string | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": false,
"full_module": "Vale.Def.PossiblyMonad",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Print_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instruction_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Bytes_Code_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": false,
"full_module": "Vale.Def.PossiblyMonad",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Print_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instruction_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Bytes_Code_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"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
}
] | false | let aux_print_reg_from_location (a:location{ALocReg? a}) : string =
let ALocReg (Reg file id) = a in
match file with
| 0 -> print_reg_name id
| 1 -> print_xmm id gcc | val aux_print_reg_from_location (a: location{ALocReg? a}) : string
let aux_print_reg_from_location (a: location{ALocReg? a}) : string = | false | null | false | let ALocReg (Reg file id) = a in
match file with
| 0 -> print_reg_name id
| 1 -> print_xmm id gcc | {
"checked_file": "Vale.Transformers.Locations.fst.checked",
"dependencies": [
"Vale.X64.Print_s.fst.checked",
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.X64.Instructions_s.fsti.checked",
"Vale.X64.Instruction_s.fsti.checked",
"Vale.X64.Bytes_Code_s.fst.checked",
"Vale.Def.PossiblyMonad.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"FStar.Universe.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Transformers.Locations.fst"
} | [
"total"
] | [
"Vale.Transformers.Locations.location",
"Prims.b2t",
"Vale.Transformers.Locations.uu___is_ALocReg",
"Vale.X64.Machine_s.reg_file_id",
"Vale.X64.Machine_s.reg_id",
"Vale.X64.Print_s.print_reg_name",
"Vale.X64.Print_s.print_xmm",
"Vale.X64.Print_s.gcc",
"Prims.string"
] | [] | module Vale.Transformers.Locations
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
open Vale.X64.Bytes_Code_s
open Vale.X64.Instruction_s
open Vale.X64.Instructions_s
open Vale.X64.Machine_Semantics_s
open Vale.X64.Machine_s
open Vale.X64.Print_s
open Vale.Def.PossiblyMonad
module L = FStar.List.Tot
(* See fsti *)
type location : eqtype =
| ALocMem : location
| ALocStack: location
| ALocReg : reg -> location
| ALocCf : location
| ALocOf : location | false | false | Vale.Transformers.Locations.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val aux_print_reg_from_location (a: location{ALocReg? a}) : string | [] | Vale.Transformers.Locations.aux_print_reg_from_location | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | a: Vale.Transformers.Locations.location{ALocReg? a} -> Prims.string | {
"end_col": 25,
"end_line": 28,
"start_col": 67,
"start_line": 24
} |
FStar.Pervasives.Lemma | val lemma_locations_complete :
s1:machine_state ->
s2:machine_state ->
flags:flags_t ->
ok:bool ->
trace:list observation ->
Lemma
(requires (
(forall a. eval_location a s1 == eval_location a s2)))
(ensures (
filter_state s1 flags ok trace ==
filter_state s2 flags ok trace)) | [
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": false,
"full_module": "Vale.Def.PossiblyMonad",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Print_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instruction_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Bytes_Code_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"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
}
] | false | let lemma_locations_complete s1 s2 flags ok trace =
let s1, s2 =
filter_state s1 flags ok trace,
filter_state s2 flags ok trace in
assert (s1.ms_ok == s2.ms_ok);
FStar.Classical.forall_intro (
(fun r ->
assert (eval_location (ALocReg r) s1 == eval_location (ALocReg r) s2) (* OBSERVE *)
) <: (r:_ -> Lemma (eval_reg r s1 == eval_reg r s2))
);
assert (FStar.FunctionalExtensionality.feq s1.ms_regs s2.ms_regs);
assert (s1.ms_regs == s2.ms_regs);
assert (overflow s1.ms_flags == overflow s2.ms_flags);
assert (cf s1.ms_flags == cf s2.ms_flags);
assert (FStar.FunctionalExtensionality.feq s1.ms_flags s2.ms_flags);
assert (s1.ms_heap == s2.ms_heap);
assert (s1.ms_stack == s2.ms_stack);
assert (s1.ms_stackTaint == s2.ms_stackTaint);
assert (s1.ms_trace == s2.ms_trace) | val lemma_locations_complete :
s1:machine_state ->
s2:machine_state ->
flags:flags_t ->
ok:bool ->
trace:list observation ->
Lemma
(requires (
(forall a. eval_location a s1 == eval_location a s2)))
(ensures (
filter_state s1 flags ok trace ==
filter_state s2 flags ok trace))
let lemma_locations_complete s1 s2 flags ok trace = | false | null | true | let s1, s2 = filter_state s1 flags ok trace, filter_state s2 flags ok trace in
assert (s1.ms_ok == s2.ms_ok);
FStar.Classical.forall_intro ((fun r ->
assert (eval_location (ALocReg r) s1 == eval_location (ALocReg r) s2))
<:
(r: _ -> Lemma (eval_reg r s1 == eval_reg r s2)));
assert (FStar.FunctionalExtensionality.feq s1.ms_regs s2.ms_regs);
assert (s1.ms_regs == s2.ms_regs);
assert (overflow s1.ms_flags == overflow s2.ms_flags);
assert (cf s1.ms_flags == cf s2.ms_flags);
assert (FStar.FunctionalExtensionality.feq s1.ms_flags s2.ms_flags);
assert (s1.ms_heap == s2.ms_heap);
assert (s1.ms_stack == s2.ms_stack);
assert (s1.ms_stackTaint == s2.ms_stackTaint);
assert (s1.ms_trace == s2.ms_trace) | {
"checked_file": "Vale.Transformers.Locations.fst.checked",
"dependencies": [
"Vale.X64.Print_s.fst.checked",
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.X64.Instructions_s.fsti.checked",
"Vale.X64.Instruction_s.fsti.checked",
"Vale.X64.Bytes_Code_s.fst.checked",
"Vale.Def.PossiblyMonad.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"FStar.Universe.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Transformers.Locations.fst"
} | [
"lemma"
] | [
"Vale.X64.Machine_Semantics_s.machine_state",
"Vale.X64.Machine_Semantics_s.flags_t",
"Prims.bool",
"Prims.list",
"Vale.X64.Machine_s.observation",
"Prims._assert",
"Prims.eq2",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_trace",
"Prims.unit",
"Vale.Arch.HeapTypes_s.memTaint_t",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint",
"Vale.X64.Machine_Semantics_s.machine_stack",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack",
"Vale.Arch.Heap.heap_impl",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap",
"FStar.FunctionalExtensionality.feq",
"Vale.X64.Machine_s.flag",
"Vale.X64.Machine_Semantics_s.flag_val_t",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_flags",
"Vale.X64.Machine_Semantics_s.cf",
"Vale.X64.Machine_Semantics_s.overflow",
"Vale.X64.Machine_Semantics_s.regs_t",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_regs",
"Vale.X64.Machine_s.reg",
"Vale.X64.Machine_s.t_reg",
"FStar.Classical.forall_intro",
"Vale.X64.Machine_Semantics_s.eval_reg",
"Vale.Transformers.Locations.location_val_t",
"Vale.Transformers.Locations.ALocReg",
"Vale.Transformers.Locations.eval_location",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.Mktuple2",
"Vale.Transformers.Locations.filter_state"
] | [] | module Vale.Transformers.Locations
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
open Vale.X64.Bytes_Code_s
open Vale.X64.Instruction_s
open Vale.X64.Instructions_s
open Vale.X64.Machine_Semantics_s
open Vale.X64.Machine_s
open Vale.X64.Print_s
open Vale.Def.PossiblyMonad
module L = FStar.List.Tot
(* See fsti *)
type location : eqtype =
| ALocMem : location
| ALocStack: location
| ALocReg : reg -> location
| ALocCf : location
| ALocOf : location
let aux_print_reg_from_location (a:location{ALocReg? a}) : string =
let ALocReg (Reg file id) = a in
match file with
| 0 -> print_reg_name id
| 1 -> print_xmm id gcc
(* See fsti *)
let disjoint_location a1 a2 =
match a1, a2 with
| ALocCf, ALocCf -> ffalse "carry flag not disjoint from itself"
| ALocOf, ALocOf -> ffalse "overflow flag not disjoint from itself"
| ALocCf, _ | ALocOf, _ | _, ALocCf | _, ALocOf -> ttrue
| ALocMem, ALocMem -> ffalse "memory not disjoint from itself"
| ALocStack, ALocStack -> ffalse "stack not disjoint from itself"
| ALocMem, ALocStack | ALocStack, ALocMem -> ttrue
| ALocReg r1, ALocReg r2 ->
(r1 <> r2) /- ("register " ^ aux_print_reg_from_location a1 ^ " not disjoint from itself")
| ALocReg _, _ | _, ALocReg _ -> ttrue
(* See fsti *)
let auto_lemma_disjoint_location a1 a2 = ()
(* See fsti *)
let downgrade_val_raise_val_u0_u1 #a x = FStar.Universe.downgrade_val_raise_val #a x
(* See fsti *)
let location_val_t a =
match a with
| ALocMem -> heap_impl
| ALocStack -> FStar.Universe.raise_t (machine_stack & memTaint_t)
| ALocReg r -> FStar.Universe.raise_t (t_reg r)
| ALocCf -> FStar.Universe.raise_t flag_val_t
| ALocOf -> FStar.Universe.raise_t flag_val_t
(* See fsti *)
let location_val_eqt a =
match a with
| ALocMem -> unit
| ALocStack -> unit
| ALocReg r -> t_reg r
| ALocCf -> flag_val_t
| ALocOf -> flag_val_t
(* See fsti *)
let eval_location a s =
match a with
| ALocMem -> s.ms_heap
| ALocStack -> FStar.Universe.raise_val (s.ms_stack, s.ms_stackTaint)
| ALocReg r -> FStar.Universe.raise_val (eval_reg r s)
| ALocCf -> FStar.Universe.raise_val (cf s.ms_flags)
| ALocOf -> FStar.Universe.raise_val (overflow s.ms_flags)
(* See fsti *)
let update_location a v s =
match a with
| ALocMem ->
let v = coerce v in
{ s with ms_heap = v }
| ALocStack ->
let v = FStar.Universe.downgrade_val (coerce v) in
{ s with ms_stack = fst v ; ms_stackTaint = snd v }
| ALocReg r ->
let v = FStar.Universe.downgrade_val v in
update_reg' r v s
| ALocCf ->
let v = FStar.Universe.downgrade_val v in
{ s with ms_flags = FStar.FunctionalExtensionality.on_dom flag (fun f -> if f = fCarry then v else s.ms_flags f) }
| ALocOf ->
let v = FStar.Universe.downgrade_val v in
{ s with ms_flags = FStar.FunctionalExtensionality.on_dom flag (fun f -> if f = fOverflow then v else s.ms_flags f) }
(* See fsti *)
let lemma_locations_truly_disjoint a a_change v s = () | false | false | Vale.Transformers.Locations.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lemma_locations_complete :
s1:machine_state ->
s2:machine_state ->
flags:flags_t ->
ok:bool ->
trace:list observation ->
Lemma
(requires (
(forall a. eval_location a s1 == eval_location a s2)))
(ensures (
filter_state s1 flags ok trace ==
filter_state s2 flags ok trace)) | [] | Vale.Transformers.Locations.lemma_locations_complete | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
s1: Vale.X64.Machine_Semantics_s.machine_state ->
s2: Vale.X64.Machine_Semantics_s.machine_state ->
flags: Vale.X64.Machine_Semantics_s.flags_t ->
ok: Prims.bool ->
trace: Prims.list Vale.X64.Machine_s.observation
-> FStar.Pervasives.Lemma
(requires
forall (a: Vale.Transformers.Locations.location).
Vale.Transformers.Locations.eval_location a s1 ==
Vale.Transformers.Locations.eval_location a s2)
(ensures
Vale.Transformers.Locations.filter_state s1 flags ok trace ==
Vale.Transformers.Locations.filter_state s2 flags ok trace) | {
"end_col": 37,
"end_line": 117,
"start_col": 51,
"start_line": 99
} |
Prims.Tot | val location_val_t : location -> Type u#1 | [
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": false,
"full_module": "Vale.Def.PossiblyMonad",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Print_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instruction_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Bytes_Code_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"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
}
] | false | let location_val_t a =
match a with
| ALocMem -> heap_impl
| ALocStack -> FStar.Universe.raise_t (machine_stack & memTaint_t)
| ALocReg r -> FStar.Universe.raise_t (t_reg r)
| ALocCf -> FStar.Universe.raise_t flag_val_t
| ALocOf -> FStar.Universe.raise_t flag_val_t | val location_val_t : location -> Type u#1
let location_val_t a = | false | null | false | match a with
| ALocMem -> heap_impl
| ALocStack -> FStar.Universe.raise_t (machine_stack & memTaint_t)
| ALocReg r -> FStar.Universe.raise_t (t_reg r)
| ALocCf -> FStar.Universe.raise_t flag_val_t
| ALocOf -> FStar.Universe.raise_t flag_val_t | {
"checked_file": "Vale.Transformers.Locations.fst.checked",
"dependencies": [
"Vale.X64.Print_s.fst.checked",
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.X64.Instructions_s.fsti.checked",
"Vale.X64.Instruction_s.fsti.checked",
"Vale.X64.Bytes_Code_s.fst.checked",
"Vale.Def.PossiblyMonad.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"FStar.Universe.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Transformers.Locations.fst"
} | [
"total"
] | [
"Vale.Transformers.Locations.location",
"Vale.Arch.Heap.heap_impl",
"FStar.Universe.raise_t",
"FStar.Pervasives.Native.tuple2",
"Vale.X64.Machine_Semantics_s.machine_stack",
"Vale.Arch.HeapTypes_s.memTaint_t",
"Vale.X64.Machine_s.reg",
"Vale.X64.Machine_s.t_reg",
"Vale.X64.Machine_Semantics_s.flag_val_t"
] | [] | module Vale.Transformers.Locations
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
open Vale.X64.Bytes_Code_s
open Vale.X64.Instruction_s
open Vale.X64.Instructions_s
open Vale.X64.Machine_Semantics_s
open Vale.X64.Machine_s
open Vale.X64.Print_s
open Vale.Def.PossiblyMonad
module L = FStar.List.Tot
(* See fsti *)
type location : eqtype =
| ALocMem : location
| ALocStack: location
| ALocReg : reg -> location
| ALocCf : location
| ALocOf : location
let aux_print_reg_from_location (a:location{ALocReg? a}) : string =
let ALocReg (Reg file id) = a in
match file with
| 0 -> print_reg_name id
| 1 -> print_xmm id gcc
(* See fsti *)
let disjoint_location a1 a2 =
match a1, a2 with
| ALocCf, ALocCf -> ffalse "carry flag not disjoint from itself"
| ALocOf, ALocOf -> ffalse "overflow flag not disjoint from itself"
| ALocCf, _ | ALocOf, _ | _, ALocCf | _, ALocOf -> ttrue
| ALocMem, ALocMem -> ffalse "memory not disjoint from itself"
| ALocStack, ALocStack -> ffalse "stack not disjoint from itself"
| ALocMem, ALocStack | ALocStack, ALocMem -> ttrue
| ALocReg r1, ALocReg r2 ->
(r1 <> r2) /- ("register " ^ aux_print_reg_from_location a1 ^ " not disjoint from itself")
| ALocReg _, _ | _, ALocReg _ -> ttrue
(* See fsti *)
let auto_lemma_disjoint_location a1 a2 = ()
(* See fsti *)
let downgrade_val_raise_val_u0_u1 #a x = FStar.Universe.downgrade_val_raise_val #a x
(* See fsti *) | false | true | Vale.Transformers.Locations.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val location_val_t : location -> Type u#1 | [] | Vale.Transformers.Locations.location_val_t | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | a: Vale.Transformers.Locations.location -> Type | {
"end_col": 47,
"end_line": 56,
"start_col": 2,
"start_line": 51
} |
Prims.Tot | val location_val_eqt : location -> eqtype | [
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": false,
"full_module": "Vale.Def.PossiblyMonad",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Print_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instruction_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Bytes_Code_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"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
}
] | false | let location_val_eqt a =
match a with
| ALocMem -> unit
| ALocStack -> unit
| ALocReg r -> t_reg r
| ALocCf -> flag_val_t
| ALocOf -> flag_val_t | val location_val_eqt : location -> eqtype
let location_val_eqt a = | false | null | false | match a with
| ALocMem -> unit
| ALocStack -> unit
| ALocReg r -> t_reg r
| ALocCf -> flag_val_t
| ALocOf -> flag_val_t | {
"checked_file": "Vale.Transformers.Locations.fst.checked",
"dependencies": [
"Vale.X64.Print_s.fst.checked",
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.X64.Instructions_s.fsti.checked",
"Vale.X64.Instruction_s.fsti.checked",
"Vale.X64.Bytes_Code_s.fst.checked",
"Vale.Def.PossiblyMonad.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"FStar.Universe.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Transformers.Locations.fst"
} | [
"total"
] | [
"Vale.Transformers.Locations.location",
"Prims.unit",
"Vale.X64.Machine_s.reg",
"Vale.X64.Machine_s.t_reg",
"Vale.X64.Machine_Semantics_s.flag_val_t",
"Prims.eqtype"
] | [] | module Vale.Transformers.Locations
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
open Vale.X64.Bytes_Code_s
open Vale.X64.Instruction_s
open Vale.X64.Instructions_s
open Vale.X64.Machine_Semantics_s
open Vale.X64.Machine_s
open Vale.X64.Print_s
open Vale.Def.PossiblyMonad
module L = FStar.List.Tot
(* See fsti *)
type location : eqtype =
| ALocMem : location
| ALocStack: location
| ALocReg : reg -> location
| ALocCf : location
| ALocOf : location
let aux_print_reg_from_location (a:location{ALocReg? a}) : string =
let ALocReg (Reg file id) = a in
match file with
| 0 -> print_reg_name id
| 1 -> print_xmm id gcc
(* See fsti *)
let disjoint_location a1 a2 =
match a1, a2 with
| ALocCf, ALocCf -> ffalse "carry flag not disjoint from itself"
| ALocOf, ALocOf -> ffalse "overflow flag not disjoint from itself"
| ALocCf, _ | ALocOf, _ | _, ALocCf | _, ALocOf -> ttrue
| ALocMem, ALocMem -> ffalse "memory not disjoint from itself"
| ALocStack, ALocStack -> ffalse "stack not disjoint from itself"
| ALocMem, ALocStack | ALocStack, ALocMem -> ttrue
| ALocReg r1, ALocReg r2 ->
(r1 <> r2) /- ("register " ^ aux_print_reg_from_location a1 ^ " not disjoint from itself")
| ALocReg _, _ | _, ALocReg _ -> ttrue
(* See fsti *)
let auto_lemma_disjoint_location a1 a2 = ()
(* See fsti *)
let downgrade_val_raise_val_u0_u1 #a x = FStar.Universe.downgrade_val_raise_val #a x
(* See fsti *)
let location_val_t a =
match a with
| ALocMem -> heap_impl
| ALocStack -> FStar.Universe.raise_t (machine_stack & memTaint_t)
| ALocReg r -> FStar.Universe.raise_t (t_reg r)
| ALocCf -> FStar.Universe.raise_t flag_val_t
| ALocOf -> FStar.Universe.raise_t flag_val_t
(* See fsti *) | false | true | Vale.Transformers.Locations.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val location_val_eqt : location -> eqtype | [] | Vale.Transformers.Locations.location_val_eqt | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | a: Vale.Transformers.Locations.location -> Prims.eqtype | {
"end_col": 24,
"end_line": 65,
"start_col": 2,
"start_line": 60
} |
Prims.Tot | val eval_location :
a:location ->
machine_state ->
location_val_t a | [
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": false,
"full_module": "Vale.Def.PossiblyMonad",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Print_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instruction_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Bytes_Code_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"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
}
] | false | let eval_location a s =
match a with
| ALocMem -> s.ms_heap
| ALocStack -> FStar.Universe.raise_val (s.ms_stack, s.ms_stackTaint)
| ALocReg r -> FStar.Universe.raise_val (eval_reg r s)
| ALocCf -> FStar.Universe.raise_val (cf s.ms_flags)
| ALocOf -> FStar.Universe.raise_val (overflow s.ms_flags) | val eval_location :
a:location ->
machine_state ->
location_val_t a
let eval_location a s = | false | null | false | match a with
| ALocMem -> s.ms_heap
| ALocStack -> FStar.Universe.raise_val (s.ms_stack, s.ms_stackTaint)
| ALocReg r -> FStar.Universe.raise_val (eval_reg r s)
| ALocCf -> FStar.Universe.raise_val (cf s.ms_flags)
| ALocOf -> FStar.Universe.raise_val (overflow s.ms_flags) | {
"checked_file": "Vale.Transformers.Locations.fst.checked",
"dependencies": [
"Vale.X64.Print_s.fst.checked",
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.X64.Instructions_s.fsti.checked",
"Vale.X64.Instruction_s.fsti.checked",
"Vale.X64.Bytes_Code_s.fst.checked",
"Vale.Def.PossiblyMonad.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"FStar.Universe.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Transformers.Locations.fst"
} | [
"total"
] | [
"Vale.Transformers.Locations.location",
"Vale.X64.Machine_Semantics_s.machine_state",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap",
"FStar.Universe.raise_val",
"FStar.Pervasives.Native.tuple2",
"Vale.X64.Machine_Semantics_s.machine_stack",
"Vale.Arch.HeapTypes_s.memTaint_t",
"FStar.Pervasives.Native.Mktuple2",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint",
"Vale.X64.Machine_s.reg",
"Vale.X64.Machine_s.t_reg",
"Vale.X64.Machine_Semantics_s.eval_reg",
"Vale.X64.Machine_Semantics_s.flag_val_t",
"Vale.X64.Machine_Semantics_s.cf",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_flags",
"Vale.X64.Machine_Semantics_s.overflow",
"Vale.Transformers.Locations.location_val_t"
] | [] | module Vale.Transformers.Locations
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
open Vale.X64.Bytes_Code_s
open Vale.X64.Instruction_s
open Vale.X64.Instructions_s
open Vale.X64.Machine_Semantics_s
open Vale.X64.Machine_s
open Vale.X64.Print_s
open Vale.Def.PossiblyMonad
module L = FStar.List.Tot
(* See fsti *)
type location : eqtype =
| ALocMem : location
| ALocStack: location
| ALocReg : reg -> location
| ALocCf : location
| ALocOf : location
let aux_print_reg_from_location (a:location{ALocReg? a}) : string =
let ALocReg (Reg file id) = a in
match file with
| 0 -> print_reg_name id
| 1 -> print_xmm id gcc
(* See fsti *)
let disjoint_location a1 a2 =
match a1, a2 with
| ALocCf, ALocCf -> ffalse "carry flag not disjoint from itself"
| ALocOf, ALocOf -> ffalse "overflow flag not disjoint from itself"
| ALocCf, _ | ALocOf, _ | _, ALocCf | _, ALocOf -> ttrue
| ALocMem, ALocMem -> ffalse "memory not disjoint from itself"
| ALocStack, ALocStack -> ffalse "stack not disjoint from itself"
| ALocMem, ALocStack | ALocStack, ALocMem -> ttrue
| ALocReg r1, ALocReg r2 ->
(r1 <> r2) /- ("register " ^ aux_print_reg_from_location a1 ^ " not disjoint from itself")
| ALocReg _, _ | _, ALocReg _ -> ttrue
(* See fsti *)
let auto_lemma_disjoint_location a1 a2 = ()
(* See fsti *)
let downgrade_val_raise_val_u0_u1 #a x = FStar.Universe.downgrade_val_raise_val #a x
(* See fsti *)
let location_val_t a =
match a with
| ALocMem -> heap_impl
| ALocStack -> FStar.Universe.raise_t (machine_stack & memTaint_t)
| ALocReg r -> FStar.Universe.raise_t (t_reg r)
| ALocCf -> FStar.Universe.raise_t flag_val_t
| ALocOf -> FStar.Universe.raise_t flag_val_t
(* See fsti *)
let location_val_eqt a =
match a with
| ALocMem -> unit
| ALocStack -> unit
| ALocReg r -> t_reg r
| ALocCf -> flag_val_t
| ALocOf -> flag_val_t
(* See fsti *) | false | false | Vale.Transformers.Locations.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val eval_location :
a:location ->
machine_state ->
location_val_t a | [] | Vale.Transformers.Locations.eval_location | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | a: Vale.Transformers.Locations.location -> s: Vale.X64.Machine_Semantics_s.machine_state
-> Vale.Transformers.Locations.location_val_t a | {
"end_col": 60,
"end_line": 74,
"start_col": 2,
"start_line": 69
} |
Prims.Tot | val disjoint_location : location -> location -> pbool | [
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": false,
"full_module": "Vale.Def.PossiblyMonad",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Print_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instruction_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Bytes_Code_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"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
}
] | false | let disjoint_location a1 a2 =
match a1, a2 with
| ALocCf, ALocCf -> ffalse "carry flag not disjoint from itself"
| ALocOf, ALocOf -> ffalse "overflow flag not disjoint from itself"
| ALocCf, _ | ALocOf, _ | _, ALocCf | _, ALocOf -> ttrue
| ALocMem, ALocMem -> ffalse "memory not disjoint from itself"
| ALocStack, ALocStack -> ffalse "stack not disjoint from itself"
| ALocMem, ALocStack | ALocStack, ALocMem -> ttrue
| ALocReg r1, ALocReg r2 ->
(r1 <> r2) /- ("register " ^ aux_print_reg_from_location a1 ^ " not disjoint from itself")
| ALocReg _, _ | _, ALocReg _ -> ttrue | val disjoint_location : location -> location -> pbool
let disjoint_location a1 a2 = | false | null | false | match a1, a2 with
| ALocCf, ALocCf -> ffalse "carry flag not disjoint from itself"
| ALocOf, ALocOf -> ffalse "overflow flag not disjoint from itself"
| ALocCf, _ | ALocOf, _ | _, ALocCf | _, ALocOf -> ttrue
| ALocMem, ALocMem -> ffalse "memory not disjoint from itself"
| ALocStack, ALocStack -> ffalse "stack not disjoint from itself"
| ALocMem, ALocStack | ALocStack, ALocMem -> ttrue
| ALocReg r1, ALocReg r2 ->
(r1 <> r2) /- ("register " ^ aux_print_reg_from_location a1 ^ " not disjoint from itself")
| ALocReg _, _ | _, ALocReg _ -> ttrue | {
"checked_file": "Vale.Transformers.Locations.fst.checked",
"dependencies": [
"Vale.X64.Print_s.fst.checked",
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.X64.Instructions_s.fsti.checked",
"Vale.X64.Instruction_s.fsti.checked",
"Vale.X64.Bytes_Code_s.fst.checked",
"Vale.Def.PossiblyMonad.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"FStar.Universe.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Transformers.Locations.fst"
} | [
"total"
] | [
"Vale.Transformers.Locations.location",
"FStar.Pervasives.Native.Mktuple2",
"Vale.Def.PossiblyMonad.ffalse",
"Vale.Def.PossiblyMonad.ttrue",
"Vale.X64.Machine_s.reg",
"Vale.Def.PossiblyMonad.op_Slash_Subtraction",
"Prims.op_disEquality",
"Prims.op_Hat",
"Vale.Transformers.Locations.aux_print_reg_from_location",
"Vale.Def.PossiblyMonad.pbool"
] | [] | module Vale.Transformers.Locations
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
open Vale.X64.Bytes_Code_s
open Vale.X64.Instruction_s
open Vale.X64.Instructions_s
open Vale.X64.Machine_Semantics_s
open Vale.X64.Machine_s
open Vale.X64.Print_s
open Vale.Def.PossiblyMonad
module L = FStar.List.Tot
(* See fsti *)
type location : eqtype =
| ALocMem : location
| ALocStack: location
| ALocReg : reg -> location
| ALocCf : location
| ALocOf : location
let aux_print_reg_from_location (a:location{ALocReg? a}) : string =
let ALocReg (Reg file id) = a in
match file with
| 0 -> print_reg_name id
| 1 -> print_xmm id gcc
(* See fsti *) | false | true | Vale.Transformers.Locations.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val disjoint_location : location -> location -> pbool | [] | Vale.Transformers.Locations.disjoint_location | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | a1: Vale.Transformers.Locations.location -> a2: Vale.Transformers.Locations.location
-> Vale.Def.PossiblyMonad.pbool | {
"end_col": 40,
"end_line": 41,
"start_col": 2,
"start_line": 32
} |
Prims.Tot | val update_location :
a:location ->
v:location_val_t a ->
machine_state ->
s:machine_state{eval_location a s == v} | [
{
"abbrev": false,
"full_module": "Vale.Arch.Heap",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapTypes_s",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": false,
"full_module": "Vale.Def.PossiblyMonad",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Print_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_Semantics_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instructions_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Instruction_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Bytes_Code_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Transformers",
"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
}
] | false | let update_location a v s =
match a with
| ALocMem ->
let v = coerce v in
{ s with ms_heap = v }
| ALocStack ->
let v = FStar.Universe.downgrade_val (coerce v) in
{ s with ms_stack = fst v ; ms_stackTaint = snd v }
| ALocReg r ->
let v = FStar.Universe.downgrade_val v in
update_reg' r v s
| ALocCf ->
let v = FStar.Universe.downgrade_val v in
{ s with ms_flags = FStar.FunctionalExtensionality.on_dom flag (fun f -> if f = fCarry then v else s.ms_flags f) }
| ALocOf ->
let v = FStar.Universe.downgrade_val v in
{ s with ms_flags = FStar.FunctionalExtensionality.on_dom flag (fun f -> if f = fOverflow then v else s.ms_flags f) } | val update_location :
a:location ->
v:location_val_t a ->
machine_state ->
s:machine_state{eval_location a s == v}
let update_location a v s = | false | null | false | match a with
| ALocMem ->
let v = coerce v in
{ s with ms_heap = v }
| ALocStack ->
let v = FStar.Universe.downgrade_val (coerce v) in
{ s with ms_stack = fst v; ms_stackTaint = snd v }
| ALocReg r ->
let v = FStar.Universe.downgrade_val v in
update_reg' r v s
| ALocCf ->
let v = FStar.Universe.downgrade_val v in
{
s with
ms_flags
=
FStar.FunctionalExtensionality.on_dom flag (fun f -> if f = fCarry then v else s.ms_flags f)
}
| ALocOf ->
let v = FStar.Universe.downgrade_val v in
{
s with
ms_flags
=
FStar.FunctionalExtensionality.on_dom flag (fun f -> if f = fOverflow then v else s.ms_flags f)
} | {
"checked_file": "Vale.Transformers.Locations.fst.checked",
"dependencies": [
"Vale.X64.Print_s.fst.checked",
"Vale.X64.Machine_Semantics_s.fst.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.X64.Instructions_s.fsti.checked",
"Vale.X64.Instruction_s.fsti.checked",
"Vale.X64.Bytes_Code_s.fst.checked",
"Vale.Def.PossiblyMonad.fst.checked",
"Vale.Arch.HeapTypes_s.fst.checked",
"Vale.Arch.Heap.fsti.checked",
"prims.fst.checked",
"FStar.Universe.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Transformers.Locations.fst"
} | [
"total"
] | [
"Vale.Transformers.Locations.location",
"Vale.Transformers.Locations.location_val_t",
"Vale.X64.Machine_Semantics_s.machine_state",
"Vale.X64.Machine_Semantics_s.Mkmachine_state",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_regs",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_flags",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_trace",
"Vale.Arch.Heap.heap_impl",
"Vale.X64.Instruction_s.coerce",
"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap",
"FStar.Pervasives.Native.fst",
"Vale.X64.Machine_Semantics_s.machine_stack",
"Vale.Arch.HeapTypes_s.memTaint_t",
"FStar.Pervasives.Native.snd",
"FStar.Pervasives.Native.tuple2",
"FStar.Universe.downgrade_val",
"FStar.Universe.raise_t",
"Vale.X64.Machine_s.reg",
"Vale.X64.Machine_Semantics_s.update_reg'",
"Vale.X64.Machine_s.t_reg",
"FStar.FunctionalExtensionality.on_dom",
"Vale.X64.Machine_s.flag",
"Vale.X64.Machine_Semantics_s.flag_val_t",
"Prims.op_Equality",
"Vale.X64.Machine_s.fCarry",
"Prims.bool",
"FStar.Pervasives.Native.option",
"Vale.X64.Machine_s.fOverflow",
"Prims.eq2",
"Vale.Transformers.Locations.eval_location"
] | [] | module Vale.Transformers.Locations
open Vale.Arch.HeapTypes_s
open Vale.Arch.Heap
open Vale.X64.Bytes_Code_s
open Vale.X64.Instruction_s
open Vale.X64.Instructions_s
open Vale.X64.Machine_Semantics_s
open Vale.X64.Machine_s
open Vale.X64.Print_s
open Vale.Def.PossiblyMonad
module L = FStar.List.Tot
(* See fsti *)
type location : eqtype =
| ALocMem : location
| ALocStack: location
| ALocReg : reg -> location
| ALocCf : location
| ALocOf : location
let aux_print_reg_from_location (a:location{ALocReg? a}) : string =
let ALocReg (Reg file id) = a in
match file with
| 0 -> print_reg_name id
| 1 -> print_xmm id gcc
(* See fsti *)
let disjoint_location a1 a2 =
match a1, a2 with
| ALocCf, ALocCf -> ffalse "carry flag not disjoint from itself"
| ALocOf, ALocOf -> ffalse "overflow flag not disjoint from itself"
| ALocCf, _ | ALocOf, _ | _, ALocCf | _, ALocOf -> ttrue
| ALocMem, ALocMem -> ffalse "memory not disjoint from itself"
| ALocStack, ALocStack -> ffalse "stack not disjoint from itself"
| ALocMem, ALocStack | ALocStack, ALocMem -> ttrue
| ALocReg r1, ALocReg r2 ->
(r1 <> r2) /- ("register " ^ aux_print_reg_from_location a1 ^ " not disjoint from itself")
| ALocReg _, _ | _, ALocReg _ -> ttrue
(* See fsti *)
let auto_lemma_disjoint_location a1 a2 = ()
(* See fsti *)
let downgrade_val_raise_val_u0_u1 #a x = FStar.Universe.downgrade_val_raise_val #a x
(* See fsti *)
let location_val_t a =
match a with
| ALocMem -> heap_impl
| ALocStack -> FStar.Universe.raise_t (machine_stack & memTaint_t)
| ALocReg r -> FStar.Universe.raise_t (t_reg r)
| ALocCf -> FStar.Universe.raise_t flag_val_t
| ALocOf -> FStar.Universe.raise_t flag_val_t
(* See fsti *)
let location_val_eqt a =
match a with
| ALocMem -> unit
| ALocStack -> unit
| ALocReg r -> t_reg r
| ALocCf -> flag_val_t
| ALocOf -> flag_val_t
(* See fsti *)
let eval_location a s =
match a with
| ALocMem -> s.ms_heap
| ALocStack -> FStar.Universe.raise_val (s.ms_stack, s.ms_stackTaint)
| ALocReg r -> FStar.Universe.raise_val (eval_reg r s)
| ALocCf -> FStar.Universe.raise_val (cf s.ms_flags)
| ALocOf -> FStar.Universe.raise_val (overflow s.ms_flags)
(* See fsti *) | false | false | Vale.Transformers.Locations.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 1,
"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": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val update_location :
a:location ->
v:location_val_t a ->
machine_state ->
s:machine_state{eval_location a s == v} | [] | Vale.Transformers.Locations.update_location | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
a: Vale.Transformers.Locations.location ->
v: Vale.Transformers.Locations.location_val_t a ->
s: Vale.X64.Machine_Semantics_s.machine_state
-> s:
Vale.X64.Machine_Semantics_s.machine_state{Vale.Transformers.Locations.eval_location a s == v} | {
"end_col": 121,
"end_line": 93,
"start_col": 2,
"start_line": 78
} |
Prims.Tot | val max_uint32: uint32_t | [
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let max_uint32 = 4294967295ul | val max_uint32: uint32_t
let max_uint32 = | false | null | false | 4294967295ul | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"FStar.UInt32.__uint_to_t"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t | false | true | LowStar.Vector.fst | {
"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"
} | null | val max_uint32: uint32_t | [] | LowStar.Vector.max_uint32 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | LowStar.Vector.uint32_t | {
"end_col": 29,
"end_line": 49,
"start_col": 17,
"start_line": 49
} |
Prims.GTot | val freeable: #a:Type -> vector a -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let freeable #a vec =
B.freeable (Vec?.vs vec) | val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec = | false | null | false | B.freeable (Vec?.vs vec) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"LowStar.Vector.vector",
"LowStar.Monotonic.Buffer.freeable",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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"
} | null | val freeable: #a:Type -> vector a -> GTot Type0 | [] | LowStar.Vector.freeable | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> Prims.GTot Type0 | {
"end_col": 26,
"end_line": 98,
"start_col": 2,
"start_line": 98
} |
Prims.Tot | val vector (a: Type0): Tot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let vector a = vector_str a | val vector (a: Type0): Tot Type0
let vector a = | false | null | false | vector_str a | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"LowStar.Vector.vector_str"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a | false | true | LowStar.Vector.fst | {
"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"
} | null | val vector (a: Type0): Tot Type0 | [] | LowStar.Vector.vector | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: Type0 -> Type0 | {
"end_col": 27,
"end_line": 62,
"start_col": 15,
"start_line": 62
} |
Prims.GTot | val is_full: #a:Type -> vstr:vector_str a -> GTot bool | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let is_full #a vstr =
Vec?.sz vstr >= max_uint32 | val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr = | false | null | false | Vec?.sz vstr >= max_uint32 | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"LowStar.Vector.vector_str",
"FStar.Integers.op_Greater_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.__proj__Vec__item__sz",
"LowStar.Vector.max_uint32",
"Prims.bool"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool | false | false | LowStar.Vector.fst | {
"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"
} | null | val is_full: #a:Type -> vstr:vector_str a -> GTot bool | [] | LowStar.Vector.is_full | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vstr: LowStar.Vector.vector_str a -> Prims.GTot Prims.bool | {
"end_col": 28,
"end_line": 88,
"start_col": 2,
"start_line": 88
} |
Prims.GTot | val live: #a:Type -> HS.mem -> vector a -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let live #a h vec =
B.live h (Vec?.vs vec) | val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec = | false | null | false | B.live h (Vec?.vs vec) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.vector",
"LowStar.Monotonic.Buffer.live",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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"
} | null | val live: #a:Type -> HS.mem -> vector a -> GTot Type0 | [] | LowStar.Vector.live | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | h: FStar.Monotonic.HyperStack.mem -> vec: LowStar.Vector.vector a -> Prims.GTot Type0 | {
"end_col": 24,
"end_line": 94,
"start_col": 2,
"start_line": 94
} |
Prims.Tot | val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let capacity_of #a vec =
Vec?.cap vec | val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
let capacity_of #a vec = | false | null | false | Vec?.cap vec | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"LowStar.Vector.vector",
"LowStar.Vector.__proj__Vec__item__cap",
"LowStar.Vector.uint32_t"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t | false | false | LowStar.Vector.fst | {
"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"
} | null | val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t | [] | LowStar.Vector.capacity_of | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> LowStar.Vector.uint32_t | {
"end_col": 14,
"end_line": 80,
"start_col": 2,
"start_line": 80
} |
Prims.Tot | val size_of: #a:Type -> vec:vector a -> Tot uint32_t | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let size_of #a vec =
Vec?.sz vec | val size_of: #a:Type -> vec:vector a -> Tot uint32_t
let size_of #a vec = | false | null | false | Vec?.sz vec | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"LowStar.Vector.vector",
"LowStar.Vector.__proj__Vec__item__sz",
"LowStar.Vector.uint32_t"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t | false | false | LowStar.Vector.fst | {
"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"
} | null | val size_of: #a:Type -> vec:vector a -> Tot uint32_t | [] | LowStar.Vector.size_of | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> LowStar.Vector.uint32_t | {
"end_col": 13,
"end_line": 76,
"start_col": 2,
"start_line": 76
} |
Prims.Tot | val resize_ratio: uint32_t | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let resize_ratio = 2ul | val resize_ratio: uint32_t
let resize_ratio = | false | null | false | 2ul | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"FStar.UInt32.__uint_to_t"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec)) | false | true | LowStar.Vector.fst | {
"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": 10,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val resize_ratio: uint32_t | [] | LowStar.Vector.resize_ratio | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | LowStar.Vector.uint32_t | {
"end_col": 30,
"end_line": 438,
"start_col": 27,
"start_line": 438
} |
Prims.GTot | val loc_vector: #a:Type -> vector a -> GTot loc | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec) | val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec = | false | null | false | B.loc_buffer (Vec?.vs vec) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"LowStar.Vector.vector",
"LowStar.Monotonic.Buffer.loc_buffer",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs",
"LowStar.Monotonic.Buffer.loc"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc | false | false | LowStar.Vector.fst | {
"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"
} | null | val loc_vector: #a:Type -> vector a -> GTot loc | [] | LowStar.Vector.loc_vector | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> Prims.GTot LowStar.Monotonic.Buffer.loc | {
"end_col": 28,
"end_line": 102,
"start_col": 2,
"start_line": 102
} |
Prims.Tot | val is_empty: #a:Type -> vec:vector a -> Tot bool | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let is_empty #a vec =
size_of vec = 0ul | val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec = | false | null | false | size_of vec = 0ul | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"LowStar.Vector.vector",
"Prims.op_Equality",
"FStar.UInt32.t",
"LowStar.Vector.size_of",
"FStar.UInt32.__uint_to_t",
"Prims.bool"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool | false | false | LowStar.Vector.fst | {
"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"
} | null | val is_empty: #a:Type -> vec:vector a -> Tot bool | [] | LowStar.Vector.is_empty | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> Prims.bool | {
"end_col": 19,
"end_line": 84,
"start_col": 2,
"start_line": 84
} |
Prims.GTot | val loc_addr_of_vector: #a:Type -> vector a -> GTot loc | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec) | val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec = | false | null | false | B.loc_addr_of_buffer (Vec?.vs vec) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"LowStar.Vector.vector",
"LowStar.Monotonic.Buffer.loc_addr_of_buffer",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs",
"LowStar.Monotonic.Buffer.loc"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc | false | false | LowStar.Vector.fst | {
"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"
} | null | val loc_addr_of_vector: #a:Type -> vector a -> GTot loc | [] | LowStar.Vector.loc_addr_of_vector | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> Prims.GTot LowStar.Monotonic.Buffer.loc | {
"end_col": 36,
"end_line": 106,
"start_col": 2,
"start_line": 106
} |
Prims.Tot | val frameOf: #a:Type -> vector a -> Tot HS.rid | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let frameOf #a vec =
B.frameOf (Vec?.vs vec) | val frameOf: #a:Type -> vector a -> Tot HS.rid
let frameOf #a vec = | false | null | false | B.frameOf (Vec?.vs vec) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"LowStar.Vector.vector",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs",
"FStar.Monotonic.HyperHeap.rid"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid | false | false | LowStar.Vector.fst | {
"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"
} | null | val frameOf: #a:Type -> vector a -> Tot HS.rid | [] | LowStar.Vector.frameOf | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> FStar.Monotonic.HyperHeap.rid | {
"end_col": 25,
"end_line": 213,
"start_col": 2,
"start_line": 213
} |
Prims.GTot | val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) | val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
let hmap_dom_eq h0 h1 = | false | null | false | Set.equal (Map.domain (HS.get_hmap h0)) (Map.domain (HS.get_hmap h1)) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Monotonic.HyperStack.mem",
"FStar.Set.equal",
"FStar.Monotonic.HyperHeap.rid",
"FStar.Map.domain",
"FStar.Monotonic.Heap.heap",
"FStar.Monotonic.HyperStack.get_hmap"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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"
} | null | val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0 | [] | LowStar.Vector.hmap_dom_eq | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | h0: FStar.Monotonic.HyperStack.mem -> h1: FStar.Monotonic.HyperStack.mem -> Prims.GTot Type0 | {
"end_col": 41,
"end_line": 218,
"start_col": 2,
"start_line": 217
} |
FStar.HyperStack.ST.ST | val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let free #a vec =
B.free (Vec?.vs vec) | val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec = | true | null | false | B.free (Vec?.vs vec) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.vector",
"LowStar.Monotonic.Buffer.free",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs",
"Prims.unit"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1)) | false | false | LowStar.Vector.fst | {
"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"
} | null | val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1)) | [] | LowStar.Vector.free | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> FStar.HyperStack.ST.ST Prims.unit | {
"end_col": 22,
"end_line": 350,
"start_col": 2,
"start_line": 350
} |
Prims.GTot | val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)}) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec)) | val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec = | false | null | false | B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec)) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.vector",
"LowStar.Monotonic.Buffer.as_seq",
"LowStar.Buffer.trivial_preorder",
"LowStar.Buffer.gsub",
"LowStar.Vector.__proj__Vec__item__vs",
"FStar.UInt32.__uint_to_t",
"LowStar.Vector.__proj__Vec__item__sz",
"FStar.Seq.Base.seq",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.l_or",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt.size",
"FStar.UInt32.n",
"FStar.Seq.Base.length",
"FStar.UInt32.v"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)}) | false | false | LowStar.Vector.fst | {
"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"
} | null | val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)}) | [] | LowStar.Vector.as_seq | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | h: FStar.Monotonic.HyperStack.mem -> vec: LowStar.Vector.vector a
-> Prims.GTot (s: FStar.Seq.Base.seq a {FStar.Seq.Base.length s = FStar.UInt32.v (Vec?.sz vec)}) | {
"end_col": 53,
"end_line": 70,
"start_col": 2,
"start_line": 70
} |
Prims.Tot | val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec) | val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size = | false | null | false | Vec new_size (Vec?.cap vec) (Vec?.vs vec) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"LowStar.Vector.Vec",
"LowStar.Vector.__proj__Vec__item__cap",
"LowStar.Vector.__proj__Vec__item__vs"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a) | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a) | [] | LowStar.Vector.shrink | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
new_size: LowStar.Vector.uint32_t{new_size <= LowStar.Vector.size_of vec}
-> LowStar.Vector.vector a | {
"end_col": 43,
"end_line": 510,
"start_col": 2,
"start_line": 510
} |
Prims.GTot | val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx) | val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p = | false | null | false | forall (idx: nat{i <= idx && idx < j}). p (S.index seq idx) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Seq.Base.seq",
"FStar.Integers.nat",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"FStar.Seq.Base.length",
"Prims.l_Forall",
"FStar.Integers.op_Less",
"FStar.Seq.Base.index"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0 | [] | LowStar.Vector.forall_seq | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
seq: FStar.Seq.Base.seq a ->
i: FStar.Integers.nat ->
j: FStar.Integers.nat{i <= j && j <= FStar.Seq.Base.length seq} ->
p: (_: a -> Prims.GTot Type0)
-> Prims.GTot Type0 | {
"end_col": 23,
"end_line": 555,
"start_col": 2,
"start_line": 554
} |
Prims.GTot | val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let get #a h vec i =
S.index (as_seq h vec) (U32.v i) | val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i = | false | null | false | S.index (as_seq h vec) (U32.v i) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"FStar.Integers.op_Less",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"FStar.Seq.Base.index",
"LowStar.Vector.as_seq",
"FStar.UInt32.v"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a | false | false | LowStar.Vector.fst | {
"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"
} | null | val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a | [] | LowStar.Vector.get | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
h: FStar.Monotonic.HyperStack.mem ->
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t{i < LowStar.Vector.size_of vec}
-> Prims.GTot a | {
"end_col": 34,
"end_line": 358,
"start_col": 2,
"start_line": 358
} |
Prims.GTot | val forall2_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> a -> GTot Type0) -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let forall2_buffer #a h buf i j p =
forall2_seq (B.as_seq h buf) i j p | val forall2_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_buffer #a h buf i j p = | false | null | false | forall2_seq (B.as_seq h buf) i j p | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Buffer.buffer",
"FStar.Integers.nat",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"LowStar.Monotonic.Buffer.length",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.forall2_seq",
"LowStar.Monotonic.Buffer.as_seq"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p
val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0
let forall_ #a h vec i j p =
forall_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0
let forall_all #a h vec p =
forall_ h vec 0ul (size_of vec) p
val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_seq #a seq i j p =
forall (k:nat{i <= k && k < j}) (l:nat{i <= l && l < j && k <> l}).
p (S.index seq k) (S.index seq l)
val forall2_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> a -> GTot Type0) -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val forall2_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> a -> GTot Type0) -> GTot Type0 | [] | LowStar.Vector.forall2_buffer | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
h: FStar.Monotonic.HyperStack.mem ->
buf: LowStar.Buffer.buffer a ->
i: FStar.Integers.nat ->
j: FStar.Integers.nat{i <= j && j <= LowStar.Monotonic.Buffer.length buf} ->
p: (_: a -> _: a -> Prims.GTot Type0)
-> Prims.GTot Type0 | {
"end_col": 36,
"end_line": 590,
"start_col": 2,
"start_line": 590
} |
FStar.HyperStack.ST.ST | val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf))) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let alloc_by_buffer #a len buf =
Vec len len buf | val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf = | true | null | false | Vec len len buf | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.uint32_t",
"Prims.b2t",
"FStar.Integers.op_Greater",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"FStar.UInt32.__uint_to_t",
"LowStar.Buffer.buffer",
"Prims.op_Equality",
"FStar.UInt32.t",
"LowStar.Monotonic.Buffer.len",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.Vec",
"LowStar.Vector.vector"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf))) | false | false | LowStar.Vector.fst | {
"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"
} | null | val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf))) | [] | LowStar.Vector.alloc_by_buffer | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
len: LowStar.Vector.uint32_t{len > 0ul} ->
buf: LowStar.Buffer.buffer a {LowStar.Monotonic.Buffer.len buf = len}
-> FStar.HyperStack.ST.ST (LowStar.Vector.vector a) | {
"end_col": 17,
"end_line": 340,
"start_col": 2,
"start_line": 340
} |
Prims.Tot | val new_capacity: cap:uint32_t -> Tot uint32_t | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio | val new_capacity: cap:uint32_t -> Tot uint32_t
let new_capacity cap = | false | null | false | if cap >= max_uint32 / resize_ratio
then max_uint32
else if cap = 0ul then 1ul else cap * resize_ratio | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"LowStar.Vector.uint32_t",
"FStar.Integers.op_Greater_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"FStar.Integers.op_Slash",
"LowStar.Vector.max_uint32",
"LowStar.Vector.resize_ratio",
"Prims.bool",
"Prims.op_Equality",
"FStar.UInt32.t",
"FStar.UInt32.__uint_to_t",
"FStar.Integers.op_Star"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t | false | true | LowStar.Vector.fst | {
"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": 10,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val new_capacity: cap:uint32_t -> Tot uint32_t | [] | LowStar.Vector.new_capacity | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | cap: LowStar.Vector.uint32_t -> LowStar.Vector.uint32_t | {
"end_col": 25,
"end_line": 444,
"start_col": 2,
"start_line": 442
} |
Prims.GTot | val forall2:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> a -> GTot Type0) -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let forall2 #a h vec i j p =
forall2_seq (as_seq h vec) (U32.v i) (U32.v j) p | val forall2:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2 #a h vec i j p = | false | null | false | forall2_seq (as_seq h vec) (U32.v i) (U32.v j) p | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"LowStar.Vector.forall2_seq",
"LowStar.Vector.as_seq",
"FStar.UInt32.v"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p
val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0
let forall_ #a h vec i j p =
forall_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0
let forall_all #a h vec p =
forall_ h vec 0ul (size_of vec) p
val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_seq #a seq i j p =
forall (k:nat{i <= k && k < j}) (l:nat{i <= l && l < j && k <> l}).
p (S.index seq k) (S.index seq l)
val forall2_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_buffer #a h buf i j p =
forall2_seq (B.as_seq h buf) i j p
val forall2:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> a -> GTot Type0) -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val forall2:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> a -> GTot Type0) -> GTot Type0 | [] | LowStar.Vector.forall2 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
h: FStar.Monotonic.HyperStack.mem ->
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t ->
j: LowStar.Vector.uint32_t{i <= j && j <= LowStar.Vector.size_of vec} ->
p: (_: a -> _: a -> Prims.GTot Type0)
-> Prims.GTot Type0 | {
"end_col": 50,
"end_line": 597,
"start_col": 2,
"start_line": 597
} |
Prims.GTot | val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p | val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p = | false | null | false | forall_seq (B.as_seq h buf) i j p | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Buffer.buffer",
"FStar.Integers.nat",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"LowStar.Monotonic.Buffer.length",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.forall_seq",
"LowStar.Monotonic.Buffer.as_seq"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0 | [] | LowStar.Vector.forall_buffer | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
h: FStar.Monotonic.HyperStack.mem ->
buf: LowStar.Buffer.buffer a ->
i: FStar.Integers.nat ->
j: FStar.Integers.nat{i <= j && j <= LowStar.Monotonic.Buffer.length buf} ->
p: (_: a -> Prims.GTot Type0)
-> Prims.GTot Type0 | {
"end_col": 35,
"end_line": 562,
"start_col": 2,
"start_line": 562
} |
Prims.GTot | val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let forall_ #a h vec i j p =
forall_seq (as_seq h vec) (U32.v i) (U32.v j) p | val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0
let forall_ #a h vec i j p = | false | null | false | forall_seq (as_seq h vec) (U32.v i) (U32.v j) p | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"LowStar.Vector.forall_seq",
"LowStar.Vector.as_seq",
"FStar.UInt32.v"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p
val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0 | [] | LowStar.Vector.forall_ | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
h: FStar.Monotonic.HyperStack.mem ->
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t ->
j: LowStar.Vector.uint32_t{i <= j && j <= LowStar.Vector.size_of vec} ->
p: (_: a -> Type0)
-> Prims.GTot Type0 | {
"end_col": 49,
"end_line": 569,
"start_col": 2,
"start_line": 569
} |
Prims.GTot | val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq)) | val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib = | false | null | false | if S.length seq = 0 then ib else fold_left_seq (S.tail seq) f (f ib (S.head seq)) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial",
""
] | [
"FStar.Seq.Base.seq",
"Prims.op_Equality",
"Prims.int",
"FStar.Seq.Base.length",
"Prims.bool",
"LowStar.Vector.fold_left_seq",
"FStar.Seq.Properties.tail",
"FStar.Seq.Properties.head"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq)) | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq)) | [
"recursion"
] | LowStar.Vector.fold_left_seq | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | seq: FStar.Seq.Base.seq a -> f: (_: b -> _: a -> Prims.GTot b) -> ib: b -> Prims.GTot b | {
"end_col": 55,
"end_line": 521,
"start_col": 2,
"start_line": 520
} |
FStar.HyperStack.ST.ST | val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len) | val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid = | true | null | false | Vec 0ul len (B.malloc rid ia len) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.uint32_t",
"Prims.b2t",
"FStar.Integers.op_Greater",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"FStar.UInt32.__uint_to_t",
"FStar.Monotonic.HyperHeap.rid",
"FStar.HyperStack.ST.is_eternal_region",
"LowStar.Vector.Vec",
"LowStar.Vector.vector",
"LowStar.Buffer.buffer",
"Prims.op_Equality",
"FStar.UInt32.t",
"LowStar.Monotonic.Buffer.len",
"LowStar.Buffer.trivial_preorder",
"LowStar.Buffer.malloc",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"LowStar.Monotonic.Buffer.length",
"FStar.UInt32.v",
"Prims.op_Negation",
"LowStar.Monotonic.Buffer.g_is_null",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.freeable"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty)) | false | false | LowStar.Vector.fst | {
"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"
} | null | val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty)) | [] | LowStar.Vector.alloc_reserve | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
len: LowStar.Vector.uint32_t{len > 0ul} ->
ia: a ->
rid: FStar.Monotonic.HyperHeap.rid{FStar.HyperStack.ST.is_eternal_region rid}
-> FStar.HyperStack.ST.ST (LowStar.Vector.vector a) | {
"end_col": 35,
"end_line": 324,
"start_col": 2,
"start_line": 324
} |
FStar.HyperStack.ST.ST | val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len) | val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid = | true | null | false | Vec len len (B.malloc rid v len) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.uint32_t",
"Prims.b2t",
"FStar.Integers.op_Greater",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"FStar.UInt32.__uint_to_t",
"FStar.Monotonic.HyperHeap.rid",
"FStar.HyperStack.ST.is_eternal_region",
"LowStar.Vector.Vec",
"LowStar.Vector.vector",
"LowStar.Buffer.buffer",
"Prims.op_Equality",
"FStar.UInt32.t",
"LowStar.Monotonic.Buffer.len",
"LowStar.Buffer.trivial_preorder",
"LowStar.Buffer.malloc",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"LowStar.Monotonic.Buffer.length",
"FStar.UInt32.v",
"Prims.op_Negation",
"LowStar.Monotonic.Buffer.g_is_null",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.freeable"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1)) | false | false | LowStar.Vector.fst | {
"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"
} | null | val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1)) | [] | LowStar.Vector.alloc_rid | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
len: LowStar.Vector.uint32_t{len > 0ul} ->
v: a ->
rid: FStar.Monotonic.HyperHeap.rid{FStar.HyperStack.ST.is_eternal_region rid}
-> FStar.HyperStack.ST.ST (LowStar.Vector.vector a) | {
"end_col": 34,
"end_line": 288,
"start_col": 2,
"start_line": 288
} |
Prims.GTot | val forall2_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> a -> GTot Type0) -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let forall2_all #a h vec p =
forall2 h vec 0ul (size_of vec) p | val forall2_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_all #a h vec p = | false | null | false | forall2 h vec 0ul (size_of vec) p | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.vector",
"LowStar.Vector.forall2",
"FStar.UInt32.__uint_to_t",
"LowStar.Vector.size_of"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p
val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0
let forall_ #a h vec i j p =
forall_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0
let forall_all #a h vec p =
forall_ h vec 0ul (size_of vec) p
val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_seq #a seq i j p =
forall (k:nat{i <= k && k < j}) (l:nat{i <= l && l < j && k <> l}).
p (S.index seq k) (S.index seq l)
val forall2_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_buffer #a h buf i j p =
forall2_seq (B.as_seq h buf) i j p
val forall2:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2 #a h vec i j p =
forall2_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall2_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> a -> GTot Type0) -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val forall2_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> a -> GTot Type0) -> GTot Type0 | [] | LowStar.Vector.forall2_all | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
h: FStar.Monotonic.HyperStack.mem ->
vec: LowStar.Vector.vector a ->
p: (_: a -> _: a -> Prims.GTot Type0)
-> Prims.GTot Type0 | {
"end_col": 35,
"end_line": 603,
"start_col": 2,
"start_line": 603
} |
FStar.HyperStack.ST.ST | val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib | val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib = | true | null | false | fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.vector",
"LowStar.Vector.fold_left_buffer",
"LowStar.Vector.__proj__Vec__item__sz",
"LowStar.Buffer.buffer",
"Prims.b2t",
"Prims.op_Equality",
"FStar.UInt32.t",
"LowStar.Monotonic.Buffer.len",
"LowStar.Buffer.trivial_preorder",
"LowStar.Buffer.sub",
"LowStar.Vector.__proj__Vec__item__vs",
"FStar.UInt32.__uint_to_t",
"FStar.Ghost.hide",
"LowStar.Monotonic.Buffer.mbuffer"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib)) | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib)) | [] | LowStar.Vector.fold_left | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> f: (_: b -> _: a -> b) -> ib: b -> FStar.HyperStack.ST.ST b | {
"end_col": 77,
"end_line": 547,
"start_col": 2,
"start_line": 547
} |
FStar.Pervasives.Lemma | val forall_preserved:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
forall_ h0 vec i j p /\
modifies dloc h0 h1))
(ensures (forall_ h1 vec i j p)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let forall_preserved #a vec i j p dloc h0 h1 =
modifies_as_seq_within vec i j dloc h0 h1;
assert (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)) | val forall_preserved:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
forall_ h0 vec i j p /\
modifies dloc h0 h1))
(ensures (forall_ h1 vec i j p))
let forall_preserved #a vec i j p dloc h0 h1 = | false | null | true | modifies_as_seq_within vec i j dloc h0 h1;
assert (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) == S.slice (as_seq h1 vec) (U32.v i) (U32.v j)) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma"
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"LowStar.Monotonic.Buffer.loc",
"FStar.Monotonic.HyperStack.mem",
"Prims._assert",
"Prims.eq2",
"FStar.Seq.Base.seq",
"FStar.Seq.Base.slice",
"LowStar.Vector.as_seq",
"FStar.UInt32.v",
"Prims.unit",
"LowStar.Vector.modifies_as_seq_within"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p
val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0
let forall_ #a h vec i j p =
forall_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0
let forall_all #a h vec p =
forall_ h vec 0ul (size_of vec) p
val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_seq #a seq i j p =
forall (k:nat{i <= k && k < j}) (l:nat{i <= l && l < j && k <> l}).
p (S.index seq k) (S.index seq l)
val forall2_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_buffer #a h buf i j p =
forall2_seq (B.as_seq h buf) i j p
val forall2:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2 #a h vec i j p =
forall2_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall2_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_all #a h vec p =
forall2 h vec 0ul (size_of vec) p
/// Facts
val get_as_seq_index:
#a:Type -> h:HS.mem -> buf:B.buffer a -> i:uint32_t{i < B.len buf} ->
Lemma (B.get h buf (U32.v i) == S.index (B.as_seq h (B.gsub buf i 1ul)) 0)
let get_as_seq_index #a h buf i = ()
val get_preserved:
#a:Type -> vec:vector a ->
i:uint32_t{i < size_of vec} ->
p:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint p (loc_vector_within vec i (i + 1ul)) /\
modifies p h0 h1))
(ensures (get h0 vec i == get h1 vec i))
let get_preserved #a vec i p h0 h1 =
get_as_seq_index h0 (Vec?.vs vec) i;
get_as_seq_index h1 (Vec?.vs vec) i
private val get_preserved_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
k:uint32_t{(k < i || j <= k) && k < size_of vec} ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
modifies (loc_vector_within vec i j) h0 h1))
(ensures (get h0 vec k == get h1 vec k))
[SMTPat (live h0 vec);
SMTPat (modifies (loc_vector_within vec i j) h0 h1);
SMTPat (get h0 vec k)]
let get_preserved_within #a vec i j k h0 h1 =
if k < i then begin
loc_vector_within_disjoint vec k (k + 1ul) i j;
get_preserved vec k (loc_vector_within vec i j) h0 h1
end
else begin
loc_vector_within_disjoint vec i j k (k + 1ul);
get_preserved vec k (loc_vector_within vec i j) h0 h1
end
val forall_seq_ok:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
k:nat{i <= k && k < j} ->
p:(a -> GTot Type0) ->
Lemma (requires (forall_seq seq i j p))
(ensures (p (S.index seq k)))
let forall_seq_ok #a seq i j k p = ()
val forall2_seq_ok:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
k:nat{i <= k && k < j} -> l:nat{i <= l && l < j && k <> l} ->
p:(a -> a -> GTot Type0) ->
Lemma (requires (forall2_seq seq i j p))
(ensures (p (S.index seq k) (S.index seq l)))
let forall2_seq_ok #a seq i j k l p = ()
val forall_as_seq:
#a:Type -> s0:S.seq a -> s1:S.seq a{S.length s0 = S.length s1} ->
i:nat -> j:nat{i <= j && j <= S.length s0} ->
k:nat{i <= k && k < j} ->
p:(a -> Tot Type0) ->
Lemma (requires (p (S.index s0 k) /\ S.slice s0 i j == S.slice s1 i j))
(ensures (p (S.index s1 k)))
[SMTPat (p (S.index s0 k));
SMTPat (S.slice s0 i j);
SMTPat (S.slice s1 i j)]
let forall_as_seq #a s0 s1 i j k p =
assert (S.index (S.slice s0 i j) (k - i) ==
S.index (S.slice s1 i j) (k - i))
val forall_preserved:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
forall_ h0 vec i j p /\
modifies dloc h0 h1))
(ensures (forall_ h1 vec i j p)) | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val forall_preserved:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
forall_ h0 vec i j p /\
modifies dloc h0 h1))
(ensures (forall_ h1 vec i j p)) | [] | LowStar.Vector.forall_preserved | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t ->
j: LowStar.Vector.uint32_t{i <= j && j <= LowStar.Vector.size_of vec} ->
p: (_: a -> Type0) ->
dloc: LowStar.Monotonic.Buffer.loc ->
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
LowStar.Vector.live h0 vec /\
LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Vector.loc_vector_within vec i j) dloc /\
LowStar.Vector.forall_ h0 vec i j p /\ LowStar.Monotonic.Buffer.modifies dloc h0 h1)
(ensures LowStar.Vector.forall_ h1 vec i j p) | {
"end_col": 53,
"end_line": 690,
"start_col": 2,
"start_line": 688
} |
FStar.HyperStack.ST.ST | val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul))) | val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib = | true | null | false | if len = 0ul
then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul)) f (f ib (B.index buf 0ul))) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
""
] | [
"LowStar.Vector.uint32_t",
"LowStar.Buffer.buffer",
"Prims.b2t",
"Prims.op_Equality",
"FStar.UInt32.t",
"LowStar.Monotonic.Buffer.len",
"LowStar.Buffer.trivial_preorder",
"FStar.UInt32.__uint_to_t",
"Prims.bool",
"LowStar.Vector.fold_left_buffer",
"FStar.Integers.op_Subtraction",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Monotonic.Buffer.index",
"FStar.UInt32.sub",
"FStar.UInt32.uint_to_t",
"LowStar.Buffer.sub",
"FStar.Ghost.hide",
"LowStar.Monotonic.Buffer.mbuffer"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf)) | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf)) | [
"recursion"
] | LowStar.Vector.fold_left_buffer | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
len: LowStar.Vector.uint32_t ->
buf: LowStar.Buffer.buffer a {LowStar.Monotonic.Buffer.len buf = len} ->
f: (_: b -> _: a -> b) ->
ib: b
-> FStar.HyperStack.ST.ST b | {
"end_col": 52,
"end_line": 536,
"start_col": 2,
"start_line": 534
} |
Prims.GTot | val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let forall_all #a h vec p =
forall_ h vec 0ul (size_of vec) p | val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0
let forall_all #a h vec p = | false | null | false | forall_ h vec 0ul (size_of vec) p | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.vector",
"LowStar.Vector.forall_",
"FStar.UInt32.__uint_to_t",
"LowStar.Vector.size_of"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p
val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0
let forall_ #a h vec i j p =
forall_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0 | [] | LowStar.Vector.forall_all | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | h: FStar.Monotonic.HyperStack.mem -> vec: LowStar.Vector.vector a -> p: (_: a -> Type0)
-> Prims.GTot Type0 | {
"end_col": 35,
"end_line": 575,
"start_col": 2,
"start_line": 575
} |
FStar.HyperStack.ST.ST | val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let alloc #a len v =
alloc_rid len v HS.root | val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v = | true | null | false | alloc_rid len v HS.root | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.uint32_t",
"Prims.b2t",
"FStar.Integers.op_Greater",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"FStar.UInt32.__uint_to_t",
"LowStar.Vector.alloc_rid",
"FStar.Monotonic.HyperHeap.root",
"LowStar.Vector.vector"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1)) | false | false | LowStar.Vector.fst | {
"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"
} | null | val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1)) | [] | LowStar.Vector.alloc | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | len: LowStar.Vector.uint32_t{len > 0ul} -> v: a -> FStar.HyperStack.ST.ST (LowStar.Vector.vector a) | {
"end_col": 25,
"end_line": 306,
"start_col": 2,
"start_line": 306
} |
FStar.HyperStack.ST.ST | val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let front #a vec =
B.index (Vec?.vs vec) 0ul | val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec = | true | null | false | B.index (Vec?.vs vec) 0ul | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.vector",
"Prims.b2t",
"FStar.Integers.op_Greater",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"FStar.UInt32.__uint_to_t",
"LowStar.Monotonic.Buffer.index",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v)) | false | false | LowStar.Vector.fst | {
"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"
} | null | val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v)) | [] | LowStar.Vector.front | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a {LowStar.Vector.size_of vec > 0ul} -> FStar.HyperStack.ST.ST a | {
"end_col": 27,
"end_line": 376,
"start_col": 2,
"start_line": 376
} |
FStar.Pervasives.Lemma | val forall_as_seq:
#a:Type -> s0:S.seq a -> s1:S.seq a{S.length s0 = S.length s1} ->
i:nat -> j:nat{i <= j && j <= S.length s0} ->
k:nat{i <= k && k < j} ->
p:(a -> Tot Type0) ->
Lemma (requires (p (S.index s0 k) /\ S.slice s0 i j == S.slice s1 i j))
(ensures (p (S.index s1 k)))
[SMTPat (p (S.index s0 k));
SMTPat (S.slice s0 i j);
SMTPat (S.slice s1 i j)] | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let forall_as_seq #a s0 s1 i j k p =
assert (S.index (S.slice s0 i j) (k - i) ==
S.index (S.slice s1 i j) (k - i)) | val forall_as_seq:
#a:Type -> s0:S.seq a -> s1:S.seq a{S.length s0 = S.length s1} ->
i:nat -> j:nat{i <= j && j <= S.length s0} ->
k:nat{i <= k && k < j} ->
p:(a -> Tot Type0) ->
Lemma (requires (p (S.index s0 k) /\ S.slice s0 i j == S.slice s1 i j))
(ensures (p (S.index s1 k)))
[SMTPat (p (S.index s0 k));
SMTPat (S.slice s0 i j);
SMTPat (S.slice s1 i j)]
let forall_as_seq #a s0 s1 i j k p = | false | null | true | assert (S.index (S.slice s0 i j) (k - i) == S.index (S.slice s1 i j) (k - i)) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma"
] | [
"FStar.Seq.Base.seq",
"Prims.b2t",
"Prims.op_Equality",
"Prims.nat",
"FStar.Seq.Base.length",
"FStar.Integers.nat",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"FStar.Integers.op_Less",
"Prims._assert",
"Prims.eq2",
"FStar.Seq.Base.index",
"FStar.Seq.Base.slice",
"FStar.Integers.op_Subtraction",
"Prims.unit"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p
val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0
let forall_ #a h vec i j p =
forall_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0
let forall_all #a h vec p =
forall_ h vec 0ul (size_of vec) p
val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_seq #a seq i j p =
forall (k:nat{i <= k && k < j}) (l:nat{i <= l && l < j && k <> l}).
p (S.index seq k) (S.index seq l)
val forall2_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_buffer #a h buf i j p =
forall2_seq (B.as_seq h buf) i j p
val forall2:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2 #a h vec i j p =
forall2_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall2_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_all #a h vec p =
forall2 h vec 0ul (size_of vec) p
/// Facts
val get_as_seq_index:
#a:Type -> h:HS.mem -> buf:B.buffer a -> i:uint32_t{i < B.len buf} ->
Lemma (B.get h buf (U32.v i) == S.index (B.as_seq h (B.gsub buf i 1ul)) 0)
let get_as_seq_index #a h buf i = ()
val get_preserved:
#a:Type -> vec:vector a ->
i:uint32_t{i < size_of vec} ->
p:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint p (loc_vector_within vec i (i + 1ul)) /\
modifies p h0 h1))
(ensures (get h0 vec i == get h1 vec i))
let get_preserved #a vec i p h0 h1 =
get_as_seq_index h0 (Vec?.vs vec) i;
get_as_seq_index h1 (Vec?.vs vec) i
private val get_preserved_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
k:uint32_t{(k < i || j <= k) && k < size_of vec} ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
modifies (loc_vector_within vec i j) h0 h1))
(ensures (get h0 vec k == get h1 vec k))
[SMTPat (live h0 vec);
SMTPat (modifies (loc_vector_within vec i j) h0 h1);
SMTPat (get h0 vec k)]
let get_preserved_within #a vec i j k h0 h1 =
if k < i then begin
loc_vector_within_disjoint vec k (k + 1ul) i j;
get_preserved vec k (loc_vector_within vec i j) h0 h1
end
else begin
loc_vector_within_disjoint vec i j k (k + 1ul);
get_preserved vec k (loc_vector_within vec i j) h0 h1
end
val forall_seq_ok:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
k:nat{i <= k && k < j} ->
p:(a -> GTot Type0) ->
Lemma (requires (forall_seq seq i j p))
(ensures (p (S.index seq k)))
let forall_seq_ok #a seq i j k p = ()
val forall2_seq_ok:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
k:nat{i <= k && k < j} -> l:nat{i <= l && l < j && k <> l} ->
p:(a -> a -> GTot Type0) ->
Lemma (requires (forall2_seq seq i j p))
(ensures (p (S.index seq k) (S.index seq l)))
let forall2_seq_ok #a seq i j k l p = ()
val forall_as_seq:
#a:Type -> s0:S.seq a -> s1:S.seq a{S.length s0 = S.length s1} ->
i:nat -> j:nat{i <= j && j <= S.length s0} ->
k:nat{i <= k && k < j} ->
p:(a -> Tot Type0) ->
Lemma (requires (p (S.index s0 k) /\ S.slice s0 i j == S.slice s1 i j))
(ensures (p (S.index s1 k)))
[SMTPat (p (S.index s0 k));
SMTPat (S.slice s0 i j);
SMTPat (S.slice s1 i j)] | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val forall_as_seq:
#a:Type -> s0:S.seq a -> s1:S.seq a{S.length s0 = S.length s1} ->
i:nat -> j:nat{i <= j && j <= S.length s0} ->
k:nat{i <= k && k < j} ->
p:(a -> Tot Type0) ->
Lemma (requires (p (S.index s0 k) /\ S.slice s0 i j == S.slice s1 i j))
(ensures (p (S.index s1 k)))
[SMTPat (p (S.index s0 k));
SMTPat (S.slice s0 i j);
SMTPat (S.slice s1 i j)] | [] | LowStar.Vector.forall_as_seq | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
s0: FStar.Seq.Base.seq a ->
s1: FStar.Seq.Base.seq a {FStar.Seq.Base.length s0 = FStar.Seq.Base.length s1} ->
i: FStar.Integers.nat ->
j: FStar.Integers.nat{i <= j && j <= FStar.Seq.Base.length s0} ->
k: FStar.Integers.nat{i <= k && k < j} ->
p: (_: a -> Type0)
-> FStar.Pervasives.Lemma
(requires
p (FStar.Seq.Base.index s0 k) /\ FStar.Seq.Base.slice s0 i j == FStar.Seq.Base.slice s1 i j)
(ensures p (FStar.Seq.Base.index s1 k))
[
SMTPat (p (FStar.Seq.Base.index s0 k));
SMTPat (FStar.Seq.Base.slice s0 i j);
SMTPat (FStar.Seq.Base.slice s1 i j)
] | {
"end_col": 42,
"end_line": 675,
"start_col": 2,
"start_line": 674
} |
FStar.HyperStack.ST.ST | val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul) | val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec = | true | null | false | B.index (Vec?.vs vec) (size_of vec - 1ul) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.vector",
"Prims.b2t",
"FStar.Integers.op_Greater",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"FStar.UInt32.__uint_to_t",
"LowStar.Monotonic.Buffer.index",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs",
"FStar.Integers.op_Subtraction"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v)) | false | false | LowStar.Vector.fst | {
"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"
} | null | val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v)) | [] | LowStar.Vector.back | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a {LowStar.Vector.size_of vec > 0ul} -> FStar.HyperStack.ST.ST a | {
"end_col": 43,
"end_line": 385,
"start_col": 2,
"start_line": 385
} |
FStar.HyperStack.ST.ST | val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let index #a vec i =
B.index (Vec?.vs vec) i | val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i = | true | null | false | B.index (Vec?.vs vec) i | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"LowStar.Monotonic.Buffer.index",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v)) | false | false | LowStar.Vector.fst | {
"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"
} | null | val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v)) | [] | LowStar.Vector.index | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> i: LowStar.Vector.uint32_t -> FStar.HyperStack.ST.ST a | {
"end_col": 25,
"end_line": 367,
"start_col": 2,
"start_line": 367
} |
FStar.Pervasives.Lemma | val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2))) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2 | val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 = | false | null | true | if i2 = j2 then () else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2 | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma",
""
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"FStar.Integers.op_Less",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"LowStar.Vector.size_of",
"Prims.op_Equality",
"Prims.l_or",
"Prims.bool",
"LowStar.Vector.loc_vector_within_disjoint_",
"FStar.Integers.op_Plus",
"FStar.UInt32.__uint_to_t",
"Prims.unit"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2))) | false | false | LowStar.Vector.fst | {
"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"
} | null | val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2))) | [
"recursion"
] | LowStar.Vector.loc_vector_within_disjoint_ | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i1: LowStar.Vector.uint32_t ->
i2: LowStar.Vector.uint32_t{i1 < i2} ->
j2: LowStar.Vector.uint32_t{i2 <= j2 && j2 <= LowStar.Vector.size_of vec}
-> FStar.Pervasives.Lemma
(ensures
LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Monotonic.Buffer.loc_buffer (LowStar.Buffer.gsub
(Vec?.vs vec)
i1
1ul))
(LowStar.Vector.loc_vector_within vec i2 j2)) (decreases FStar.UInt32.v (j2 - i2)) | {
"end_col": 55,
"end_line": 179,
"start_col": 2,
"start_line": 178
} |
FStar.Pervasives.Lemma | val get_preserved:
#a:Type -> vec:vector a ->
i:uint32_t{i < size_of vec} ->
p:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint p (loc_vector_within vec i (i + 1ul)) /\
modifies p h0 h1))
(ensures (get h0 vec i == get h1 vec i)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let get_preserved #a vec i p h0 h1 =
get_as_seq_index h0 (Vec?.vs vec) i;
get_as_seq_index h1 (Vec?.vs vec) i | val get_preserved:
#a:Type -> vec:vector a ->
i:uint32_t{i < size_of vec} ->
p:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint p (loc_vector_within vec i (i + 1ul)) /\
modifies p h0 h1))
(ensures (get h0 vec i == get h1 vec i))
let get_preserved #a vec i p h0 h1 = | false | null | true | get_as_seq_index h0 (Vec?.vs vec) i;
get_as_seq_index h1 (Vec?.vs vec) i | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma"
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"FStar.Integers.op_Less",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"LowStar.Monotonic.Buffer.loc",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.get_as_seq_index",
"LowStar.Vector.__proj__Vec__item__vs",
"Prims.unit"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p
val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0
let forall_ #a h vec i j p =
forall_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0
let forall_all #a h vec p =
forall_ h vec 0ul (size_of vec) p
val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_seq #a seq i j p =
forall (k:nat{i <= k && k < j}) (l:nat{i <= l && l < j && k <> l}).
p (S.index seq k) (S.index seq l)
val forall2_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_buffer #a h buf i j p =
forall2_seq (B.as_seq h buf) i j p
val forall2:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2 #a h vec i j p =
forall2_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall2_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_all #a h vec p =
forall2 h vec 0ul (size_of vec) p
/// Facts
val get_as_seq_index:
#a:Type -> h:HS.mem -> buf:B.buffer a -> i:uint32_t{i < B.len buf} ->
Lemma (B.get h buf (U32.v i) == S.index (B.as_seq h (B.gsub buf i 1ul)) 0)
let get_as_seq_index #a h buf i = ()
val get_preserved:
#a:Type -> vec:vector a ->
i:uint32_t{i < size_of vec} ->
p:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint p (loc_vector_within vec i (i + 1ul)) /\
modifies p h0 h1))
(ensures (get h0 vec i == get h1 vec i)) | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_preserved:
#a:Type -> vec:vector a ->
i:uint32_t{i < size_of vec} ->
p:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint p (loc_vector_within vec i (i + 1ul)) /\
modifies p h0 h1))
(ensures (get h0 vec i == get h1 vec i)) | [] | LowStar.Vector.get_preserved | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t{i < LowStar.Vector.size_of vec} ->
p: LowStar.Monotonic.Buffer.loc ->
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
LowStar.Vector.live h0 vec /\
LowStar.Monotonic.Buffer.loc_disjoint p (LowStar.Vector.loc_vector_within vec i (i + 1ul)) /\
LowStar.Monotonic.Buffer.modifies p h0 h1)
(ensures LowStar.Vector.get h0 vec i == LowStar.Vector.get h1 vec i) | {
"end_col": 37,
"end_line": 622,
"start_col": 2,
"start_line": 621
} |
FStar.Pervasives.Lemma | val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1))) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2) | val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 = | false | null | true | if i1 = j1
then ()
else
(loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma",
""
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"Prims.op_Equality",
"Prims.bool",
"LowStar.Vector.loc_vector_within_disjoint",
"FStar.Integers.op_Plus",
"FStar.UInt32.__uint_to_t",
"Prims.unit",
"LowStar.Vector.loc_vector_within_disjoint_"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1))) | false | false | LowStar.Vector.fst | {
"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"
} | null | val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1))) | [
"recursion"
] | LowStar.Vector.loc_vector_within_disjoint | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i1: LowStar.Vector.uint32_t ->
j1: LowStar.Vector.uint32_t{i1 <= j1 && j1 <= LowStar.Vector.size_of vec} ->
i2: LowStar.Vector.uint32_t{j1 <= i2} ->
j2: LowStar.Vector.uint32_t{i2 <= j2 && j2 <= LowStar.Vector.size_of vec}
-> FStar.Pervasives.Lemma
(ensures
LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Vector.loc_vector_within vec i1 j1)
(LowStar.Vector.loc_vector_within vec i2 j2)) (decreases FStar.UInt32.v (j1 - i1)) | {
"end_col": 58,
"end_line": 192,
"start_col": 2,
"start_line": 190
} |
FStar.Pervasives.Lemma | val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i))) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end | val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j = | false | null | true | if i = j - 1ul
then ()
else
(loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma",
""
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"FStar.Integers.op_Less_Equals",
"LowStar.Vector.size_of",
"Prims.op_Equality",
"FStar.UInt32.t",
"FStar.Integers.op_Subtraction",
"FStar.UInt32.__uint_to_t",
"Prims.bool",
"LowStar.Monotonic.Buffer.loc_union_assoc",
"LowStar.Monotonic.Buffer.loc_buffer",
"LowStar.Buffer.trivial_preorder",
"LowStar.Buffer.gsub",
"LowStar.Vector.__proj__Vec__item__vs",
"LowStar.Vector.loc_vector_within",
"FStar.Integers.op_Plus",
"Prims.unit",
"LowStar.Vector.loc_vector_within_union_rev"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i))) | false | false | LowStar.Vector.fst | {
"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"
} | null | val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i))) | [
"recursion"
] | LowStar.Vector.loc_vector_within_union_rev | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t ->
j: LowStar.Vector.uint32_t{i < j && j <= LowStar.Vector.size_of vec}
-> FStar.Pervasives.Lemma
(ensures
LowStar.Vector.loc_vector_within vec i j ==
LowStar.Monotonic.Buffer.loc_union (LowStar.Vector.loc_vector_within vec i (j - 1ul))
(LowStar.Vector.loc_vector_within vec (j - 1ul) j)) (decreases FStar.UInt32.v (j - i)) | {
"end_col": 5,
"end_line": 209,
"start_col": 2,
"start_line": 203
} |
FStar.Pervasives.Lemma | val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i))) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j | val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j = | false | null | true | if i = j then () else loc_vector_within_included vec (i + 1ul) j | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma",
""
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"Prims.op_Equality",
"Prims.bool",
"LowStar.Vector.loc_vector_within_included",
"FStar.Integers.op_Plus",
"FStar.UInt32.__uint_to_t",
"Prims.unit"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i))) | false | false | LowStar.Vector.fst | {
"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"
} | null | val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i))) | [
"recursion"
] | LowStar.Vector.loc_vector_within_included | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t ->
j: LowStar.Vector.uint32_t{i <= j && j <= LowStar.Vector.size_of vec}
-> FStar.Pervasives.Lemma
(ensures
LowStar.Monotonic.Buffer.loc_includes (LowStar.Vector.loc_vector vec)
(LowStar.Vector.loc_vector_within vec i j)) (decreases FStar.UInt32.v (j - i)) | {
"end_col": 49,
"end_line": 166,
"start_col": 2,
"start_line": 165
} |
FStar.Pervasives.Lemma | val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1))) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end | val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 = | false | null | true | if i1 = j1
then ()
else
if i1 = i2
then loc_vector_within_includes_ vec i1 j1 j2
else
(loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma",
""
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"Prims.op_Equality",
"Prims.bool",
"LowStar.Vector.loc_vector_within_includes_",
"LowStar.Monotonic.Buffer.loc_includes_union_l",
"LowStar.Monotonic.Buffer.loc_buffer",
"LowStar.Buffer.trivial_preorder",
"LowStar.Buffer.gsub",
"LowStar.Vector.__proj__Vec__item__vs",
"FStar.UInt32.__uint_to_t",
"LowStar.Vector.loc_vector_within",
"FStar.Integers.op_Plus",
"Prims.unit",
"LowStar.Vector.loc_vector_within_includes"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1))) | false | false | LowStar.Vector.fst | {
"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"
} | null | val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1))) | [
"recursion"
] | LowStar.Vector.loc_vector_within_includes | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i1: LowStar.Vector.uint32_t ->
j1: LowStar.Vector.uint32_t{i1 <= j1 && j1 <= LowStar.Vector.size_of vec} ->
i2: LowStar.Vector.uint32_t{i1 <= i2} ->
j2: LowStar.Vector.uint32_t{i2 <= j2 && j2 <= j1}
-> FStar.Pervasives.Lemma
(ensures
LowStar.Monotonic.Buffer.loc_includes (LowStar.Vector.loc_vector_within vec i1 j1)
(LowStar.Vector.loc_vector_within vec i2 j2)) (decreases FStar.UInt32.v (j1 - i1)) | {
"end_col": 5,
"end_line": 155,
"start_col": 2,
"start_line": 148
} |
FStar.Pervasives.Lemma | val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)] | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1 | val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 = | false | null | true | B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1 | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma"
] | [
"LowStar.Vector.vector",
"LowStar.Monotonic.Buffer.loc",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Monotonic.Buffer.modifies_buffer_elim",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs",
"Prims.unit"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)] | false | false | LowStar.Vector.fst | {
"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"
} | null | val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)] | [] | LowStar.Vector.modifies_as_seq | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
dloc: LowStar.Monotonic.Buffer.loc ->
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
LowStar.Vector.live h0 vec /\
LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Vector.loc_vector vec) dloc /\
LowStar.Monotonic.Buffer.modifies dloc h0 h1)
(ensures
LowStar.Vector.live h1 vec /\ LowStar.Vector.as_seq h0 vec == LowStar.Vector.as_seq h1 vec)
[
SMTPat (LowStar.Vector.live h0 vec);
SMTPat (LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Vector.loc_vector vec) dloc);
SMTPat (LowStar.Monotonic.Buffer.modifies dloc h0 h1)
] | {
"end_col": 49,
"end_line": 232,
"start_col": 2,
"start_line": 232
} |
Prims.GTot | val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i))) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j) | val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j = | false | null | false | if i = j
then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul)) (loc_vector_within vec (i + 1ul) j) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial",
""
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"Prims.op_Equality",
"LowStar.Monotonic.Buffer.loc_none",
"Prims.bool",
"LowStar.Monotonic.Buffer.loc_union",
"LowStar.Monotonic.Buffer.loc_buffer",
"LowStar.Buffer.trivial_preorder",
"LowStar.Buffer.gsub",
"LowStar.Vector.__proj__Vec__item__vs",
"FStar.UInt32.__uint_to_t",
"LowStar.Vector.loc_vector_within",
"FStar.Integers.op_Plus",
"LowStar.Monotonic.Buffer.loc"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i))) | false | false | LowStar.Vector.fst | {
"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"
} | null | val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i))) | [
"recursion"
] | LowStar.Vector.loc_vector_within | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t ->
j: LowStar.Vector.uint32_t{i <= j && j <= LowStar.Vector.size_of vec}
-> Prims.GTot LowStar.Monotonic.Buffer.loc | {
"end_col": 52,
"end_line": 115,
"start_col": 2,
"start_line": 113
} |
FStar.Pervasives.Lemma | val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)] | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end | val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 = | false | null | true | if i = j
then ()
else
(B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma",
""
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"LowStar.Monotonic.Buffer.loc",
"FStar.Monotonic.HyperStack.mem",
"Prims.op_Equality",
"Prims.bool",
"Prims._assert",
"FStar.Seq.Base.equal",
"FStar.Seq.Base.slice",
"LowStar.Vector.as_seq",
"FStar.UInt32.v",
"FStar.Seq.Base.append",
"FStar.Integers.op_Plus",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"Prims.unit",
"LowStar.Vector.modifies_as_seq_within",
"FStar.UInt32.__uint_to_t",
"LowStar.Monotonic.Buffer.modifies_buffer_elim",
"LowStar.Buffer.trivial_preorder",
"LowStar.Buffer.gsub",
"LowStar.Vector.__proj__Vec__item__vs"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)] | false | false | LowStar.Vector.fst | {
"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"
} | null | val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)] | [
"recursion"
] | LowStar.Vector.modifies_as_seq_within | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t ->
j: LowStar.Vector.uint32_t{i <= j && j <= LowStar.Vector.size_of vec} ->
dloc: LowStar.Monotonic.Buffer.loc ->
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
LowStar.Vector.live h0 vec /\
LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Vector.loc_vector_within vec i j) dloc /\
LowStar.Monotonic.Buffer.modifies dloc h0 h1)
(ensures
FStar.Seq.Base.slice (LowStar.Vector.as_seq h0 vec) (FStar.UInt32.v i) (FStar.UInt32.v j) ==
FStar.Seq.Base.slice (LowStar.Vector.as_seq h1 vec) (FStar.UInt32.v i) (FStar.UInt32.v j))
(decreases FStar.UInt32.v (j - i))
[
SMTPat (LowStar.Vector.live h0 vec);
SMTPat (LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Vector.loc_vector_within vec i j)
dloc);
SMTPat (LowStar.Monotonic.Buffer.modifies dloc h0 h1)
] | {
"end_col": 5,
"end_line": 258,
"start_col": 2,
"start_line": 248
} |
FStar.HyperStack.ST.ST | val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec))))) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs | val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i = | true | null | false | let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"LowStar.Vector.Vec",
"Prims.unit",
"LowStar.Monotonic.Buffer.free",
"LowStar.Buffer.trivial_preorder",
"LowStar.Monotonic.Buffer.blit",
"FStar.UInt32.__uint_to_t",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"LowStar.Monotonic.Buffer.length",
"FStar.UInt32.v",
"Prims.op_Negation",
"LowStar.Monotonic.Buffer.g_is_null",
"FStar.Monotonic.HyperHeap.rid",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.freeable",
"LowStar.Buffer.malloc",
"LowStar.Buffer.buffer",
"Prims.op_Equality",
"FStar.UInt32.t",
"LowStar.Monotonic.Buffer.len",
"LowStar.Vector.__proj__Vec__item__cap",
"LowStar.Vector.__proj__Vec__item__vs",
"FStar.UInt32.gte",
"LowStar.Vector.__proj__Vec__item__sz",
"Prims.bool",
"FStar.Integers.op_Greater_Equals",
"FStar.Integers.int_t",
"FStar.Integers.op_Subtraction"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec) | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec))))) | [] | LowStar.Vector.flush | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> ia: a -> i: LowStar.Vector.uint32_t{i <= LowStar.Vector.size_of vec}
-> FStar.HyperStack.ST.ST (LowStar.Vector.vector a) | {
"end_col": 17,
"end_line": 503,
"start_col": 23,
"start_line": 496
} |
FStar.Pervasives.Lemma | val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i))) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end | val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 = | false | null | true | if i = j1
then ()
else
if i = j2
then ()
else
(loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma",
""
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"Prims.op_Equality",
"Prims.bool",
"LowStar.Monotonic.Buffer.loc_includes_union_r",
"LowStar.Vector.loc_vector_within",
"LowStar.Monotonic.Buffer.loc_buffer",
"LowStar.Buffer.trivial_preorder",
"LowStar.Buffer.gsub",
"LowStar.Vector.__proj__Vec__item__vs",
"FStar.UInt32.__uint_to_t",
"FStar.Integers.op_Plus",
"Prims.unit",
"LowStar.Monotonic.Buffer.loc_includes_union_l",
"LowStar.Vector.loc_vector_within_includes_"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i))) | false | false | LowStar.Vector.fst | {
"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"
} | null | val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i))) | [
"recursion"
] | LowStar.Vector.loc_vector_within_includes_ | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t ->
j1: LowStar.Vector.uint32_t{i <= j1 && j1 <= LowStar.Vector.size_of vec} ->
j2: LowStar.Vector.uint32_t{i <= j2 && j2 <= j1}
-> FStar.Pervasives.Lemma
(ensures
LowStar.Monotonic.Buffer.loc_includes (LowStar.Vector.loc_vector_within vec i j1)
(LowStar.Vector.loc_vector_within vec i j2)) (decreases FStar.UInt32.v (j1 - i)) | {
"end_col": 5,
"end_line": 137,
"start_col": 2,
"start_line": 127
} |
Prims.Tot | val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul}) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let alloc_empty a =
Vec 0ul 0ul B.null | val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
let alloc_empty a = | false | null | false | Vec 0ul 0ul B.null | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"LowStar.Vector.Vec",
"FStar.UInt32.__uint_to_t",
"LowStar.Buffer.null",
"LowStar.Vector.vector",
"Prims.b2t",
"Prims.op_Equality",
"FStar.UInt32.t",
"LowStar.Vector.size_of"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul}) | false | false | LowStar.Vector.fst | {
"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"
} | null | val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul}) | [] | LowStar.Vector.alloc_empty | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: Type0 -> vec: LowStar.Vector.vector a {LowStar.Vector.size_of vec = 0ul} | {
"end_col": 20,
"end_line": 265,
"start_col": 2,
"start_line": 265
} |
FStar.HyperStack.ST.ST | val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec)) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec)) | val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
let assign #a vec i v = | true | null | false | let hh0 = HST.get () in
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec)) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"LowStar.Vector.slice_append",
"FStar.Seq.Base.upd",
"LowStar.Vector.as_seq",
"FStar.UInt32.v",
"FStar.Integers.op_Plus",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"LowStar.Vector.size_of",
"Prims.unit",
"LowStar.Vector.modifies_as_seq_within",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"FStar.UInt32.__uint_to_t",
"LowStar.Vector.loc_vector_within",
"LowStar.Vector.loc_vector_within_disjoint",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"LowStar.Monotonic.Buffer.upd",
"LowStar.Buffer.trivial_preorder",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Buffer.sub",
"LowStar.Vector.__proj__Vec__item__vs",
"FStar.Ghost.hide",
"FStar.UInt32.t"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec)) | false | false | LowStar.Vector.fst | {
"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": 10,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec)) | [] | LowStar.Vector.assign | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> i: LowStar.Vector.uint32_t -> v: a
-> FStar.HyperStack.ST.ST Prims.unit | {
"end_col": 89,
"end_line": 435,
"start_col": 23,
"start_line": 420
} |
Prims.Tot | val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul}) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec) | val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec = | false | null | false | Vec 0ul (Vec?.cap vec) (Vec?.vs vec) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"total"
] | [
"LowStar.Vector.vector",
"LowStar.Vector.Vec",
"FStar.UInt32.__uint_to_t",
"LowStar.Vector.__proj__Vec__item__cap",
"LowStar.Vector.__proj__Vec__item__vs",
"Prims.b2t",
"Prims.op_Equality",
"FStar.UInt32.t",
"LowStar.Vector.size_of"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul}) | false | false | LowStar.Vector.fst | {
"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"
} | null | val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul}) | [] | LowStar.Vector.clear | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> cvec: LowStar.Vector.vector a {LowStar.Vector.size_of cvec = 0ul} | {
"end_col": 38,
"end_line": 393,
"start_col": 2,
"start_line": 393
} |
Prims.GTot | val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0 | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let forall2_seq #a seq i j p =
forall (k:nat{i <= k && k < j}) (l:nat{i <= l && l < j && k <> l}).
p (S.index seq k) (S.index seq l) | val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_seq #a seq i j p = | false | null | false | forall (k: nat{i <= k && k < j}) (l: nat{i <= l && l < j && k <> l}).
p (S.index seq k) (S.index seq l) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"sometrivial"
] | [
"FStar.Seq.Base.seq",
"FStar.Integers.nat",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"FStar.Seq.Base.length",
"Prims.l_Forall",
"FStar.Integers.op_Less",
"Prims.op_disEquality",
"FStar.Seq.Base.index"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p
val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0
let forall_ #a h vec i j p =
forall_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0
let forall_all #a h vec p =
forall_ h vec 0ul (size_of vec) p
val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0 | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0 | [] | LowStar.Vector.forall2_seq | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
seq: FStar.Seq.Base.seq a ->
i: FStar.Integers.nat ->
j: FStar.Integers.nat{i <= j && j <= FStar.Seq.Base.length seq} ->
p: (_: a -> _: a -> Prims.GTot Type0)
-> Prims.GTot Type0 | {
"end_col": 37,
"end_line": 583,
"start_col": 2,
"start_line": 582
} |
FStar.Pervasives.Lemma | val get_preserved_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
k:uint32_t{(k < i || j <= k) && k < size_of vec} ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
modifies (loc_vector_within vec i j) h0 h1))
(ensures (get h0 vec k == get h1 vec k))
[SMTPat (live h0 vec);
SMTPat (modifies (loc_vector_within vec i j) h0 h1);
SMTPat (get h0 vec k)] | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let get_preserved_within #a vec i j k h0 h1 =
if k < i then begin
loc_vector_within_disjoint vec k (k + 1ul) i j;
get_preserved vec k (loc_vector_within vec i j) h0 h1
end
else begin
loc_vector_within_disjoint vec i j k (k + 1ul);
get_preserved vec k (loc_vector_within vec i j) h0 h1
end | val get_preserved_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
k:uint32_t{(k < i || j <= k) && k < size_of vec} ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
modifies (loc_vector_within vec i j) h0 h1))
(ensures (get h0 vec k == get h1 vec k))
[SMTPat (live h0 vec);
SMTPat (modifies (loc_vector_within vec i j) h0 h1);
SMTPat (get h0 vec k)]
let get_preserved_within #a vec i j k h0 h1 = | false | null | true | if k < i
then
(loc_vector_within_disjoint vec k (k + 1ul) i j;
get_preserved vec k (loc_vector_within vec i j) h0 h1)
else
(loc_vector_within_disjoint vec i j k (k + 1ul);
get_preserved vec k (loc_vector_within vec i j) h0 h1) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [
"lemma"
] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"Prims.b2t",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"LowStar.Vector.size_of",
"Prims.op_BarBar",
"FStar.Integers.op_Less",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.get_preserved",
"LowStar.Vector.loc_vector_within",
"Prims.unit",
"LowStar.Vector.loc_vector_within_disjoint",
"FStar.Integers.op_Plus",
"FStar.UInt32.__uint_to_t",
"Prims.bool"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
#reset-options "--z3rlimit 20"
let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs)
// Flush elements in the vector until the index `i`.
// It also frees the original allocation and reallocates a smaller space for
// remaining elements.
val flush:
#a:Type -> vec:vector a -> ia:a ->
i:uint32_t{i <= size_of vec} ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 fvec h1 ->
frameOf vec = frameOf fvec /\
hmap_dom_eq h0 h1 /\
live h1 fvec /\ freeable fvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector fvec)) h0 h1 /\
size_of fvec = size_of vec - i /\
S.equal (as_seq h1 fvec)
(S.slice (as_seq h0 vec) (U32.v i) (U32.v (size_of vec)))))
let flush #a vec ia i =
let fsz = Vec?.sz vec - i in
let asz = if Vec?.sz vec = i then 1ul else fsz in
let vs = Vec?.vs vec in
let fvs = B.malloc (B.frameOf vs) ia asz in
B.blit vs i fvs 0ul fsz;
B.free vs;
Vec fsz asz fvs
val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a)
let shrink #a vec new_size =
Vec new_size (Vec?.cap vec) (Vec?.vs vec)
/// Iteration
val fold_left_seq:
#a:Type -> #b:Type0 -> seq:S.seq a ->
f:(b -> a -> GTot b) -> ib:b ->
GTot b (decreases (S.length seq))
let rec fold_left_seq #a #b seq f ib =
if S.length seq = 0 then ib
else fold_left_seq (S.tail seq) f (f ib (S.head seq))
val fold_left_buffer:
#a:Type -> #b:Type0 -> len:uint32_t ->
buf:B.buffer a{B.len buf = len} ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (B.as_seq h0 buf) f ib))
(decreases (B.length buf))
let rec fold_left_buffer #a #b len buf f ib =
if len = 0ul then ib
else (fold_left_buffer (len - 1ul) (B.sub buf 1ul (len - 1ul))
f (f ib (B.index buf 0ul)))
val fold_left:
#a:Type -> #b:Type0 -> vec:vector a ->
f:(b -> a -> Tot b) -> ib:b ->
HST.ST b
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\
v == fold_left_seq (as_seq h0 vec) f ib))
let fold_left #a #b vec f ib =
fold_left_buffer (Vec?.sz vec) (B.sub (Vec?.vs vec) 0ul (Vec?.sz vec)) f ib
val forall_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_seq #a seq i j p =
forall (idx:nat{i <= idx && idx < j}).
p (S.index seq idx)
val forall_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> GTot Type0) -> GTot Type0
let forall_buffer #a h buf i j p =
forall_seq (B.as_seq h buf) i j p
val forall_:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> Tot Type0) -> GTot Type0
let forall_ #a h vec i j p =
forall_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0
let forall_all #a h vec p =
forall_ h vec 0ul (size_of vec) p
val forall2_seq:
#a:Type -> seq:S.seq a ->
i:nat -> j:nat{i <= j && j <= S.length seq} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_seq #a seq i j p =
forall (k:nat{i <= k && k < j}) (l:nat{i <= l && l < j && k <> l}).
p (S.index seq k) (S.index seq l)
val forall2_buffer:
#a:Type -> h:HS.mem -> buf:B.buffer a ->
i:nat -> j:nat{i <= j && j <= B.length buf} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_buffer #a h buf i j p =
forall2_seq (B.as_seq h buf) i j p
val forall2:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2 #a h vec i j p =
forall2_seq (as_seq h vec) (U32.v i) (U32.v j) p
val forall2_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> a -> GTot Type0) -> GTot Type0
let forall2_all #a h vec p =
forall2 h vec 0ul (size_of vec) p
/// Facts
val get_as_seq_index:
#a:Type -> h:HS.mem -> buf:B.buffer a -> i:uint32_t{i < B.len buf} ->
Lemma (B.get h buf (U32.v i) == S.index (B.as_seq h (B.gsub buf i 1ul)) 0)
let get_as_seq_index #a h buf i = ()
val get_preserved:
#a:Type -> vec:vector a ->
i:uint32_t{i < size_of vec} ->
p:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint p (loc_vector_within vec i (i + 1ul)) /\
modifies p h0 h1))
(ensures (get h0 vec i == get h1 vec i))
let get_preserved #a vec i p h0 h1 =
get_as_seq_index h0 (Vec?.vs vec) i;
get_as_seq_index h1 (Vec?.vs vec) i
private val get_preserved_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
k:uint32_t{(k < i || j <= k) && k < size_of vec} ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
modifies (loc_vector_within vec i j) h0 h1))
(ensures (get h0 vec k == get h1 vec k))
[SMTPat (live h0 vec);
SMTPat (modifies (loc_vector_within vec i j) h0 h1);
SMTPat (get h0 vec k)] | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_preserved_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
k:uint32_t{(k < i || j <= k) && k < size_of vec} ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
modifies (loc_vector_within vec i j) h0 h1))
(ensures (get h0 vec k == get h1 vec k))
[SMTPat (live h0 vec);
SMTPat (modifies (loc_vector_within vec i j) h0 h1);
SMTPat (get h0 vec k)] | [] | LowStar.Vector.get_preserved_within | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t ->
j: LowStar.Vector.uint32_t{i <= j && j <= LowStar.Vector.size_of vec} ->
k: LowStar.Vector.uint32_t{(k < i || j <= k) && k < LowStar.Vector.size_of vec} ->
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
LowStar.Vector.live h0 vec /\
LowStar.Monotonic.Buffer.modifies (LowStar.Vector.loc_vector_within vec i j) h0 h1)
(ensures LowStar.Vector.get h0 vec k == LowStar.Vector.get h1 vec k)
[
SMTPat (LowStar.Vector.live h0 vec);
SMTPat (LowStar.Monotonic.Buffer.modifies (LowStar.Vector.loc_vector_within vec i j) h0 h1);
SMTPat (LowStar.Vector.get h0 vec k)
] | {
"end_col": 5,
"end_line": 643,
"start_col": 2,
"start_line": 636
} |
FStar.HyperStack.ST.ST | val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v))) | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"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
}
] | false | let insert #a vec v =
let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then (let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs) | val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v)))
let insert #a vec v = | true | null | false | let sz = Vec?.sz vec in
let cap = Vec?.cap vec in
let vs = Vec?.vs vec in
if sz = cap
then
(let ncap = new_capacity cap in
let nvs = B.malloc (B.frameOf vs) v ncap in
B.blit vs 0ul nvs 0ul sz;
B.upd nvs sz v;
B.free vs;
Vec (sz + 1ul) ncap nvs)
else
(B.upd vs sz v;
Vec (sz + 1ul) cap vs) | {
"checked_file": "LowStar.Vector.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.Integers.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowStar.Vector.fst"
} | [] | [
"LowStar.Vector.vector",
"Prims.op_Equality",
"LowStar.Vector.uint32_t",
"LowStar.Vector.Vec",
"FStar.Integers.op_Plus",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"FStar.UInt32.__uint_to_t",
"Prims.unit",
"LowStar.Monotonic.Buffer.free",
"LowStar.Buffer.trivial_preorder",
"LowStar.Monotonic.Buffer.upd",
"LowStar.Monotonic.Buffer.blit",
"LowStar.Monotonic.Buffer.mbuffer",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"LowStar.Monotonic.Buffer.length",
"FStar.UInt32.v",
"Prims.b2t",
"Prims.op_Negation",
"LowStar.Monotonic.Buffer.g_is_null",
"FStar.Monotonic.HyperHeap.rid",
"LowStar.Monotonic.Buffer.frameOf",
"LowStar.Monotonic.Buffer.freeable",
"LowStar.Buffer.malloc",
"LowStar.Vector.new_capacity",
"Prims.bool",
"LowStar.Buffer.buffer",
"FStar.UInt32.t",
"LowStar.Monotonic.Buffer.len",
"LowStar.Vector.__proj__Vec__item__cap",
"LowStar.Vector.__proj__Vec__item__vs",
"FStar.UInt32.gte",
"LowStar.Vector.__proj__Vec__item__sz"
] | [] | (*
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.Vector
(**
* This module provides support for mutable, partially filled arrays
* whose contents may grow up to some fixed capacity determined
* during initialization.
*
* Vectors support an insertion operation that may, if the capacity
* has been reached, involve copying its contents to a new
* vector of twice the capacity (so long as the capacity has not
* already reached max_uint32).
*
* Conversely, an operation called `flush` is also provided to
* shrink a vector to some prefix of its current contents.
*
* Other operations are fairly standard, and involve reading,
* writing, and iterating over a vector.
*
*)
open FStar.Integers
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module B = LowStar.Buffer
module S = FStar.Seq
type uint32_t = UInt32.t
val max_uint32: uint32_t
let max_uint32 = 4294967295ul // UInt32.uint_to_t (UInt.max_int UInt32.n)
module U32 = FStar.UInt32
/// Abstract vector type
inline_for_extraction noeq type vector_str a =
| Vec: sz:uint32_t ->
cap:uint32_t{cap >= sz} ->
vs:B.buffer a{B.len vs = cap} ->
vector_str a
val vector (a: Type0): Tot Type0
let vector a = vector_str a
/// Specification
val as_seq:
HS.mem -> #a:Type -> vec:vector a ->
GTot (s:S.seq a{S.length s = U32.v (Vec?.sz vec)})
let as_seq h #a vec =
B.as_seq h (B.gsub (Vec?.vs vec) 0ul (Vec?.sz vec))
/// Capacity
inline_for_extraction val size_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let size_of #a vec =
Vec?.sz vec
inline_for_extraction val capacity_of: #a:Type -> vec:vector a -> Tot uint32_t
inline_for_extraction let capacity_of #a vec =
Vec?.cap vec
val is_empty: #a:Type -> vec:vector a -> Tot bool
let is_empty #a vec =
size_of vec = 0ul
val is_full: #a:Type -> vstr:vector_str a -> GTot bool
let is_full #a vstr =
Vec?.sz vstr >= max_uint32
/// Memory-related
val live: #a:Type -> HS.mem -> vector a -> GTot Type0
let live #a h vec =
B.live h (Vec?.vs vec)
val freeable: #a:Type -> vector a -> GTot Type0
let freeable #a vec =
B.freeable (Vec?.vs vec)
val loc_vector: #a:Type -> vector a -> GTot loc
let loc_vector #a vec =
B.loc_buffer (Vec?.vs vec)
val loc_addr_of_vector: #a:Type -> vector a -> GTot loc
let loc_addr_of_vector #a vec =
B.loc_addr_of_buffer (Vec?.vs vec)
val loc_vector_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
GTot loc (decreases (U32.v (j - i)))
let rec loc_vector_within #a vec i j =
if i = j then loc_none
else loc_union (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j)
val loc_vector_within_includes_:
#a:Type -> vec:vector a ->
i:uint32_t ->
j1:uint32_t{i <= j1 && j1 <= size_of vec} ->
j2:uint32_t{i <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i j1)
(loc_vector_within vec i j2)))
(decreases (U32.v (j1 - i)))
let rec loc_vector_within_includes_ #a vec i j1 j2 =
if i = j1 then ()
else if i = j2 then ()
else begin
loc_vector_within_includes_ vec (i + 1ul) j1 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j1)
(loc_vector_within vec (i + 1ul) j2);
loc_includes_union_r (loc_vector_within vec i j1)
(B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) j2)
end
val loc_vector_within_includes:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{i1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= j1} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_includes #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else if i1 = i2 then loc_vector_within_includes_ vec i1 j1 j2
else begin
loc_vector_within_includes vec (i1 + 1ul) j1 i2 j2;
loc_includes_union_l (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec (i1 + 1ul) j1)
(loc_vector_within vec i2 j2)
end
val loc_vector_within_included:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_includes (loc_vector vec)
(loc_vector_within vec i j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_included #a vec i j =
if i = j then ()
else loc_vector_within_included vec (i + 1ul) j
val loc_vector_within_disjoint_:
#a:Type -> vec:vector a ->
i1:uint32_t ->
i2:uint32_t{i1 < i2} ->
j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (B.loc_buffer (B.gsub (Vec?.vs vec) i1 1ul))
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j2 - i2)))
let rec loc_vector_within_disjoint_ #a vec i1 i2 j2 =
if i2 = j2 then ()
else loc_vector_within_disjoint_ vec i1 (i2 + 1ul) j2
val loc_vector_within_disjoint:
#a:Type -> vec:vector a ->
i1:uint32_t -> j1:uint32_t{i1 <= j1 && j1 <= size_of vec} ->
i2:uint32_t{j1 <= i2} -> j2:uint32_t{i2 <= j2 && j2 <= size_of vec} ->
Lemma (requires True)
(ensures (loc_disjoint (loc_vector_within vec i1 j1)
(loc_vector_within vec i2 j2)))
(decreases (U32.v (j1 - i1)))
let rec loc_vector_within_disjoint #a vec i1 j1 i2 j2 =
if i1 = j1 then ()
else (loc_vector_within_disjoint_ vec i1 i2 j2;
loc_vector_within_disjoint vec (i1 + 1ul) j1 i2 j2)
val loc_vector_within_union_rev:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i < j && j <= size_of vec} ->
Lemma (requires True)
(ensures (loc_vector_within vec i j ==
loc_union (loc_vector_within vec i (j - 1ul))
(loc_vector_within vec (j - 1ul) j)))
(decreases (U32.v (j - i)))
let rec loc_vector_within_union_rev #a vec i j =
if i = j - 1ul then ()
else begin
loc_vector_within_union_rev vec (i + 1ul) j;
loc_union_assoc (B.loc_buffer (B.gsub (Vec?.vs vec) i 1ul))
(loc_vector_within vec (i + 1ul) (j - 1ul))
(loc_vector_within vec (j - 1ul) j)
end
unfold val frameOf: #a:Type -> vector a -> Tot HS.rid
unfold let frameOf #a vec =
B.frameOf (Vec?.vs vec)
unfold val hmap_dom_eq: h0:HS.mem -> h1:HS.mem -> GTot Type0
unfold let hmap_dom_eq h0 h1 =
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1))
val modifies_as_seq:
#a:Type -> vec:vector a -> dloc:loc ->
h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector vec) dloc /\
modifies dloc h0 h1))
(ensures (live h1 vec /\
as_seq h0 vec == as_seq h1 vec))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector vec) dloc);
SMTPat (modifies dloc h0 h1)]
let modifies_as_seq #a vec dloc h0 h1 =
B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1
val modifies_as_seq_within:
#a:Type -> vec:vector a ->
i:uint32_t -> j:uint32_t{i <= j && j <= size_of vec} ->
dloc:loc -> h0:HS.mem -> h1:HS.mem ->
Lemma (requires (live h0 vec /\
loc_disjoint (loc_vector_within vec i j) dloc /\
modifies dloc h0 h1))
(ensures (S.slice (as_seq h0 vec) (U32.v i) (U32.v j) ==
S.slice (as_seq h1 vec) (U32.v i) (U32.v j)))
(decreases (U32.v (j - i)))
[SMTPat (live h0 vec);
SMTPat (loc_disjoint (loc_vector_within vec i j) dloc);
SMTPat (modifies dloc h0 h1)]
let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
if i = j then ()
else begin
B.modifies_buffer_elim (B.gsub (Vec?.vs vec) i 1ul) dloc h0 h1;
modifies_as_seq_within vec (i + 1ul) j dloc h0 h1;
assert (S.equal (S.slice (as_seq h0 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h0 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h0 vec) (U32.v i + 1) (U32.v j))));
assert (S.equal (S.slice (as_seq h1 vec) (U32.v i) (U32.v j))
(S.append (S.slice (as_seq h1 vec) (U32.v i) (U32.v i + 1))
(S.slice (as_seq h1 vec) (U32.v i + 1) (U32.v j))))
end
/// Construction
inline_for_extraction val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul})
inline_for_extraction let alloc_empty a =
Vec 0ul 0ul B.null
val alloc_empty_as_seq_empty:
a:Type -> h:HS.mem ->
Lemma (S.equal (as_seq h (alloc_empty a)) S.empty)
[SMTPat (as_seq h (alloc_empty a))]
let alloc_empty_as_seq_empty a h = ()
val alloc_rid:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc_rid #a len v rid =
Vec len len (B.malloc rid v len)
// Allocate a vector with the length `len`, filled with the initial value `v`.
// Note that the vector is allocated in the root region.
val alloc:
#a:Type -> len:uint32_t{len > 0ul} -> v:a ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = HS.root /\
live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = len /\
S.equal (as_seq h1 vec) (S.create (U32.v len) v) /\
B.fresh_loc (loc_vector vec) h0 h1))
let alloc #a len v =
alloc_rid len v HS.root
// Allocate a vector with the _capacity_ `len`; we still need to provide an
// initial value `ia` in order to allocate space.
val alloc_reserve:
#a:Type -> len:uint32_t{len > 0ul} -> ia:a ->
rid:HS.rid{HST.is_eternal_region rid} ->
HST.ST (vector a)
(requires (fun h0 -> true))
(ensures (fun h0 vec h1 ->
frameOf vec = rid /\ live h1 vec /\ freeable vec /\
modifies loc_none h0 h1 /\
B.fresh_loc (loc_vector vec) h0 h1 /\
Set.equal (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
size_of vec = 0ul /\
S.equal (as_seq h1 vec) S.empty))
let alloc_reserve #a len ia rid =
Vec 0ul len (B.malloc rid ia len)
// Allocate a vector with a given buffer with the length `len`.
// Note that it does not copy the buffer content; instead it directly uses the
// buffer pointer.
val alloc_by_buffer:
#a:Type -> len:uint32_t{len > 0ul} ->
buf:B.buffer a{B.len buf = len} ->
HST.ST (vector a)
(requires (fun h0 -> B.live h0 buf))
(ensures (fun h0 vec h1 ->
frameOf vec = B.frameOf buf /\ loc_vector vec == B.loc_buffer buf /\
live h1 vec /\ h0 == h1 /\
size_of vec = len /\
S.equal (as_seq h1 vec) (B.as_seq h0 buf)))
let alloc_by_buffer #a len buf =
Vec len len buf
/// Destruction
val free:
#a:Type -> vec:vector a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ freeable vec))
(ensures (fun h0 _ h1 -> modifies (loc_addr_of_vector vec) h0 h1))
let free #a vec =
B.free (Vec?.vs vec)
/// Element access
val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a
let get #a h vec i =
S.index (as_seq h vec) (U32.v i)
val index:
#a:Type -> vec:vector a -> i:uint32_t ->
HST.ST a
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v i) == v))
let index #a vec i =
B.index (Vec?.vs vec) i
val front:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) 0 == v))
let front #a vec =
B.index (Vec?.vs vec) 0ul
val back:
#a:Type -> vec:vector a{size_of vec > 0ul} ->
HST.ST a
(requires (fun h0 -> live h0 vec))
(ensures (fun h0 v h1 ->
h0 == h1 /\ S.index (as_seq h1 vec) (U32.v (size_of vec) - 1) == v))
let back #a vec =
B.index (Vec?.vs vec) (size_of vec - 1ul)
/// Operations
val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul})
let clear #a vec =
Vec 0ul (Vec?.cap vec) (Vec?.vs vec)
val clear_as_seq_empty:
#a:Type -> h:HS.mem -> vec:vector a ->
Lemma (S.equal (as_seq h (clear vec)) S.empty)
[SMTPat (as_seq h (clear vec))]
let clear_as_seq_empty #a h vec = ()
private val slice_append:
#a:Type -> s:S.seq a ->
i:nat -> j:nat{i <= j} -> k:nat{j <= k && k <= S.length s} ->
Lemma (S.equal (S.slice s i k)
(S.append (S.slice s i j) (S.slice s j k)))
private let slice_append #a s i j k = ()
val assign:
#a:Type -> vec:vector a ->
i:uint32_t -> v:a ->
HST.ST unit
(requires (fun h0 -> live h0 vec /\ i < size_of vec))
(ensures (fun h0 _ h1 ->
hmap_dom_eq h0 h1 /\
modifies (loc_vector_within #a vec i (i + 1ul)) h0 h1 /\
get h1 vec i == v /\
S.equal (as_seq h1 vec) (S.upd (as_seq h0 vec) (U32.v i) v) /\
live h1 vec))
#reset-options "--z3rlimit 10"
let assign #a vec i v =
let hh0 = HST.get () in
// NOTE: `B.upd (Vec?.vs vec) i v` makes more sense,
// but the `modifies` postcondition is coarse-grained.
B.upd (B.sub (Vec?.vs vec) i 1ul) 0ul v;
let hh1 = HST.get () in
loc_vector_within_disjoint vec 0ul i i (i + 1ul);
modifies_as_seq_within
vec 0ul i (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
loc_vector_within_disjoint vec i (i + 1ul) (i + 1ul) (size_of vec);
modifies_as_seq_within
vec (i + 1ul) (size_of vec) (loc_vector_within #a vec i (i + 1ul)) hh0 hh1;
slice_append (as_seq hh1 vec) 0 (U32.v i) (U32.v i + 1);
slice_append (as_seq hh1 vec) 0 (U32.v i + 1) (U32.v (size_of vec));
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i) (U32.v i + 1);
slice_append (S.upd (as_seq hh0 vec) (U32.v i) v) 0 (U32.v i + 1) (U32.v (size_of vec))
private val resize_ratio: uint32_t
private let resize_ratio = 2ul
private val new_capacity: cap:uint32_t -> Tot uint32_t
private let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio
val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v))) | false | false | LowStar.Vector.fst | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val insert:
#a:Type -> vec:vector a -> v:a ->
HST.ST (vector a)
(requires (fun h0 ->
live h0 vec /\ freeable vec /\ not (is_full vec) /\
HST.is_eternal_region (frameOf vec)))
(ensures (fun h0 nvec h1 ->
frameOf vec = frameOf nvec /\
hmap_dom_eq h0 h1 /\
live h1 nvec /\ freeable nvec /\
modifies (loc_union (loc_addr_of_vector vec)
(loc_vector nvec)) h0 h1 /\
size_of nvec = size_of vec + 1ul /\
get h1 nvec (size_of vec) == v /\
S.equal (as_seq h1 nvec) (S.snoc (as_seq h0 vec) v))) | [] | LowStar.Vector.insert | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | vec: LowStar.Vector.vector a -> v: a -> FStar.HyperStack.ST.ST (LowStar.Vector.vector a) | {
"end_col": 26,
"end_line": 475,
"start_col": 21,
"start_line": 462
} |
Prims.Tot | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let elemB = lbuffer uint64 5ul | let elemB = | false | null | false | lbuffer uint64 5ul | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [
"total"
] | [
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint64",
"FStar.UInt32.__uint_to_t"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0" | false | true | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val elemB : Type0 | [] | Hacl.Impl.Ed25519.RecoverX.elemB | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | Type0 | {
"end_col": 30,
"end_line": 21,
"start_col": 12,
"start_line": 21
} |
|
FStar.HyperStack.ST.Stack | val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let x_mod_2 x =
let x0 = x.(0ul) in
let z = x0 &. u64 1 in
mod_mask_lemma x0 1ul;
Lib.IntTypes.Compatibility.uintv_extensionality (u64 1) (mod_mask #U64 1ul);
z | val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2)
let x_mod_2 x = | true | null | false | let x0 = x.(0ul) in
let z = x0 &. u64 1 in
mod_mask_lemma x0 1ul;
Lib.IntTypes.Compatibility.uintv_extensionality (u64 1) (mod_mask #U64 1ul);
z | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Hacl.Bignum25519.felem",
"Prims.unit",
"Lib.IntTypes.Compatibility.uintv_extensionality",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"Lib.IntTypes.u64",
"Lib.IntTypes.mod_mask",
"FStar.UInt32.__uint_to_t",
"Lib.IntTypes.mod_mask_lemma",
"Lib.IntTypes.int_t",
"Lib.IntTypes.op_Amp_Dot",
"Lib.IntTypes.uint64",
"Lib.Buffer.op_Array_Access",
"Lib.Buffer.MUT"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline]
let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL)
inline_for_extraction noextract
val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime)))
let gte_q x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL &&
u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL)
val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1)
[@CInline]
let mul_modp_sqrt_m1 x =
[@inline_let] let (x0, x1, x2, x3, x4) =
(u64 0x00061b274a0ea0b0,
u64 0x0000d5a5fc8f189d,
u64 0x0007ef5e9cbd0c60,
u64 0x00078595a6804c9e,
u64 0x0002b8324804fc1d) in
push_frame();
let sqrt_m1 = create 5ul (u64 0) in
make_u64_5 sqrt_m1 x0 x1 x2 x3 x4;
assert_norm (S51.as_nat5 (x0, x1, x2, x3, x4) == SE.modp_sqrt_m1);
fmul x x sqrt_m1;
pop_frame()
inline_for_extraction noextract
val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2) | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2) | [] | Hacl.Impl.Ed25519.RecoverX.x_mod_2 | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | x: Hacl.Bignum25519.felem -> FStar.HyperStack.ST.Stack Lib.IntTypes.uint64 | {
"end_col": 3,
"end_line": 100,
"start_col": 15,
"start_line": 95
} |
FStar.HyperStack.ST.Stack | val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime))) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let gte_q x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL &&
u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL) | val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime)))
let gte_q x = | true | null | false | let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL && u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL) | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Hacl.Impl.Ed25519.RecoverX.elemB",
"Prims.op_AmpAmp",
"FStar.UInt64.op_Greater_Equals_Hat",
"Lib.RawIntTypes.u64_to_UInt64",
"FStar.UInt64.__uint_to_t",
"FStar.UInt64.op_Equals_Hat",
"Prims.bool",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"Lib.Buffer.op_Array_Access",
"Lib.Buffer.MUT",
"Lib.IntTypes.uint64",
"FStar.UInt32.__uint_to_t"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline]
let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL)
inline_for_extraction noextract
val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime))) | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime))) | [] | Hacl.Impl.Ed25519.RecoverX.gte_q | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | x: Hacl.Impl.Ed25519.RecoverX.elemB -> FStar.HyperStack.ST.Stack Prims.bool | {
"end_col": 41,
"end_line": 63,
"start_col": 2,
"start_line": 52
} |
FStar.HyperStack.ST.Stack | val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero))) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL) | val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
let is_0 x = | true | null | false | let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL && u64_to_UInt64 x1 =^ 0uL && u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL) | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Hacl.Impl.Ed25519.RecoverX.elemB",
"Prims.op_AmpAmp",
"FStar.UInt64.op_Equals_Hat",
"Lib.RawIntTypes.u64_to_UInt64",
"FStar.UInt64.__uint_to_t",
"Prims.bool",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"Lib.Buffer.op_Array_Access",
"Lib.Buffer.MUT",
"Lib.IntTypes.uint64",
"FStar.UInt32.__uint_to_t"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline] | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero))) | [] | Hacl.Impl.Ed25519.RecoverX.is_0 | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | x: Hacl.Impl.Ed25519.RecoverX.elemB -> FStar.HyperStack.ST.Stack Prims.bool | {
"end_col": 27,
"end_line": 41,
"start_col": 2,
"start_line": 30
} |
FStar.HyperStack.ST.Stack | val recover_x:
x:lbuffer uint64 5ul
-> y:lbuffer uint64 5ul
-> sign:uint64{v sign = 0 \/ v sign = 1} ->
Stack bool
(requires fun h -> live h x /\ live h y /\ disjoint x y /\
F51.mul_inv_t h x /\
F51.felem_fits h y (1, 1, 1, 1, 1)
)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
(z ==> F51.mul_inv_t h1 x) /\
(let res = SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0) in
(Some? res <==> z) /\
(Some? res ==> F51.fevalh h1 x == Some?.v res))
) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let recover_x x y sign =
push_frame();
let tmp = create 15ul (u64 0) in
let res = recover_x_ x y sign tmp in
pop_frame();
res | val recover_x:
x:lbuffer uint64 5ul
-> y:lbuffer uint64 5ul
-> sign:uint64{v sign = 0 \/ v sign = 1} ->
Stack bool
(requires fun h -> live h x /\ live h y /\ disjoint x y /\
F51.mul_inv_t h x /\
F51.felem_fits h y (1, 1, 1, 1, 1)
)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
(z ==> F51.mul_inv_t h1 x) /\
(let res = SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0) in
(Some? res <==> z) /\
(Some? res ==> F51.fevalh h1 x == Some?.v res))
)
let recover_x x y sign = | true | null | false | push_frame ();
let tmp = create 15ul (u64 0) in
let res = recover_x_ x y sign tmp in
pop_frame ();
res | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint64",
"FStar.UInt32.__uint_to_t",
"Prims.l_or",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Lib.IntTypes.v",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"Prims.bool",
"Prims.unit",
"FStar.HyperStack.ST.pop_frame",
"Hacl.Impl.Ed25519.RecoverX.recover_x_",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Lib.IntTypes.int_t",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Lib.Buffer.create",
"Lib.IntTypes.u64",
"FStar.HyperStack.ST.push_frame"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline]
let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL)
inline_for_extraction noextract
val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime)))
let gte_q x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL &&
u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL)
val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1)
[@CInline]
let mul_modp_sqrt_m1 x =
[@inline_let] let (x0, x1, x2, x3, x4) =
(u64 0x00061b274a0ea0b0,
u64 0x0000d5a5fc8f189d,
u64 0x0007ef5e9cbd0c60,
u64 0x00078595a6804c9e,
u64 0x0002b8324804fc1d) in
push_frame();
let sqrt_m1 = create 5ul (u64 0) in
make_u64_5 sqrt_m1 x0 x1 x2 x3 x4;
assert_norm (S51.as_nat5 (x0, x1, x2, x3, x4) == SE.modp_sqrt_m1);
fmul x x sqrt_m1;
pop_frame()
inline_for_extraction noextract
val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2)
let x_mod_2 x =
let x0 = x.(0ul) in
let z = x0 &. u64 1 in
mod_mask_lemma x0 1ul;
Lib.IntTypes.Compatibility.uintv_extensionality (u64 1) (mod_mask #U64 1ul);
z
inline_for_extraction noextract
val recover_x_step_1: x2:elemB -> y:elemB -> Stack unit
(requires fun h ->
live h x2 /\ live h y /\ disjoint x2 y /\
F51.mul_inv_t h y)
(ensures fun h0 _ h1 -> modifies (loc x2) h0 h1 /\
F51.fevalh h1 x2 == F51.as_nat h1 x2 /\
F51.mul_inv_t h1 x2 /\
(let y = F51.fevalh h0 y in
let x2 = F51.fevalh h1 x2 in
let y2 = y `SC.fmul` y in
x2 == (y2 `SC.fsub` SC.one) `SC.fmul` (SC.finv ((SE.d `SC.fmul` y2) `SC.fadd` SC.one))))
let recover_x_step_1 x2 y =
push_frame();
let tmp = create 20ul (u64 0) in
let one = sub tmp 0ul 5ul in
let y2 = sub tmp 5ul 5ul in
let dyyi = sub tmp 10ul 5ul in
let dyy = sub tmp 15ul 5ul in
make_one one;
fsquare y2 y; // y2 = y * y
times_d dyy y2; // dyy = d * y2
fsum dyy dyy one; // dyy = (d * y2) + one
reduce_513 dyy;
inverse dyyi dyy; // dyyi = modp_inv ((d * y2) + one)
fdifference x2 y2 one; // x2 = y2 - one
fmul x2 x2 dyyi; // x2 = (y2 - one) * dyyi
reduce x2;
pop_frame()
inline_for_extraction noextract
val recover_x_step_2: x:elemB -> sign:uint64 -> x2:elemB -> Stack uint8
(requires fun h ->
live h x2 /\ live h x /\ disjoint x x2 /\
F51.mul_inv_t h x /\ F51.fevalh h x2 == F51.as_nat h x2)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
(if F51.fevalh h0 x2 = 0 then (
if v sign = 0 then F51.fevalh h1 x = 0 /\ z == u8 1
else h0 == h1 /\ z == u8 0)
else h0 == h1 /\ z == u8 2))
let recover_x_step_2 x sign x2 =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x2_is_0 = is_0 x2 in
if x2_is_0 then begin
if u64_to_UInt64 sign =^ 0uL then (make_zero x; u8 1) else u8 0 end
else u8 2
inline_for_extraction noextract
val recover_x_step_3: tmp:lbuffer uint64 15ul -> Stack unit
(requires fun h -> live h tmp /\ F51.mul_inv_t h (gsub tmp 0ul 5ul))
(ensures fun h0 _ h1 -> modifies (loc tmp) h0 h1 /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
(let x2 = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let x = x2 `SC.fpow` ((SC.prime + 3) / 8) in
let x = if ((x `SC.fmul` x) `SC.fsub` x2) <> 0 then (x `SC.fmul` SE.modp_sqrt_m1) else x in
F51.fevalh h1 (gsub tmp 5ul 5ul) == x))
let recover_x_step_3 tmp =
let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in // x
let t0 = sub tmp 10ul 5ul in
Hacl.Impl.Ed25519.Pow2_252m2.pow2_252m2 x3 x2; // x3 = x2^((prime + 3) / 8)
fsquare t0 x3; // t0 = x3 * x3
fdifference t0 t0 x2; // t0 = t0 - x2
reduce_513 t0;
reduce t0;
let t0_is_0 = is_0 t0 in
if t0_is_0 then () else mul_modp_sqrt_m1 x3
inline_for_extraction noextract
val recover_x_step_4: tmp:lbuffer uint64 15ul -> Stack bool
(requires fun h -> live h tmp /\
F51.mul_inv_t h (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h (gsub tmp 5ul 5ul))
(ensures fun h0 z h1 -> modifies (loc tmp) h0 h1 /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
F51.fevalh h0 (gsub tmp 5ul 5ul) == F51.fevalh h1 (gsub tmp 5ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
(let u = F51.fevalh h0 (gsub tmp 5ul 5ul) in
let v = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let y = (u `SC.fmul` u) `SC.fsub` v in
(z <==> y == SC.zero)))
let recover_x_step_4 tmp =
let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in
let t0 = sub tmp 10ul 5ul in
fsquare t0 x3; // t0 = x3 * x3
fdifference t0 t0 x2; // t0 - x2
reduce_513 t0;
reduce t0;
is_0 t0
inline_for_extraction noextract
val recover_x_step_5:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack unit
(requires fun h ->
live h x /\ live h tmp /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h (gsub tmp 5ul 5ul) /\
(let y = F51.as_nat h y in
let sign = (uint_v #U64 sign <> 0) in
let x = F51.fevalh h (gsub tmp 5ul 5ul) in
let x2 = F51.fevalh h (gsub tmp 0ul 5ul) in
((x `SC.fmul` x) `SC.fsub` x2) == SC.zero /\
SE.recover_x y sign ==
(if ((x `SC.fmul` x) `SC.fsub` x2) <> SC.zero then None
else (
let x =
if (x % 2 = 1) <> sign then (SC.prime - x) % SC.prime else x in
Some x))))
(ensures fun h0 _ h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == Some?.v (SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0)))
let recover_x_step_5 x y sign tmp =
let x3 = sub tmp 5ul 5ul in
let t0 = sub tmp 10ul 5ul in
reduce x3;
let x0 = x_mod_2 x3 in
let open Lib.RawIntTypes in
let open FStar.UInt64 in
if not (u64_to_UInt64 x0 =^ u64_to_UInt64 sign) then (
let h0 = ST.get () in
make_zero t0;
fdifference x3 t0 x3; // x3 = (-x) % prime
reduce_513 x3;
reduce x3;
(**) assert_norm (SC.prime % SC.prime = SC.zero % SC.prime);
(**) FStar.Math.Lemmas.mod_add_both SC.prime SC.zero (- (F51.fevalh h0 x3)) SC.prime);
copy x x3
inline_for_extraction noextract
val recover_x_:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack bool
(requires fun h ->
live h tmp /\ live h x /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h x /\
F51.felem_fits h y (1, 1, 1, 1, 1)
)
(ensures fun h0 z h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
(z ==> F51.mul_inv_t h1 x) /\
(let res = SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0) in
(Some? res <==> z) /\
(Some? res ==> F51.fevalh h1 x == Some?.v res))
)
let recover_x_ x y sign tmp =
let x2 = sub tmp 0ul 5ul in
let open Lib.RawIntTypes in
let open FStar.UInt8 in
let h0 = ST.get() in
let b = gte_q y in
let res =
if b then false
else (
(**) FStar.Math.Lemmas.small_mod (F51.as_nat h0 y) SC.prime;
recover_x_step_1 x2 y;
let z = recover_x_step_2 x sign x2 in
if (u8_to_UInt8 z =^ 0uy) then false
else if (u8_to_UInt8 z =^ 1uy) then true
else (
recover_x_step_3 tmp;
let z = recover_x_step_4 tmp in
let h1 = ST.get() in
if z = false then false
else (
recover_x_step_5 x y sign tmp;
true)
)
) in
res
val recover_x:
x:lbuffer uint64 5ul
-> y:lbuffer uint64 5ul
-> sign:uint64{v sign = 0 \/ v sign = 1} ->
Stack bool
(requires fun h -> live h x /\ live h y /\ disjoint x y /\
F51.mul_inv_t h x /\
F51.felem_fits h y (1, 1, 1, 1, 1)
)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
(z ==> F51.mul_inv_t h1 x) /\
(let res = SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0) in
(Some? res <==> z) /\
(Some? res ==> F51.fevalh h1 x == Some?.v res))
)
[@CInline] | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val recover_x:
x:lbuffer uint64 5ul
-> y:lbuffer uint64 5ul
-> sign:uint64{v sign = 0 \/ v sign = 1} ->
Stack bool
(requires fun h -> live h x /\ live h y /\ disjoint x y /\
F51.mul_inv_t h x /\
F51.felem_fits h y (1, 1, 1, 1, 1)
)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
(z ==> F51.mul_inv_t h1 x) /\
(let res = SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0) in
(Some? res <==> z) /\
(Some? res ==> F51.fevalh h1 x == Some?.v res))
) | [] | Hacl.Impl.Ed25519.RecoverX.recover_x | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
x: Lib.Buffer.lbuffer Lib.IntTypes.uint64 5ul ->
y: Lib.Buffer.lbuffer Lib.IntTypes.uint64 5ul ->
sign: Lib.IntTypes.uint64{Lib.IntTypes.v sign = 0 \/ Lib.IntTypes.v sign = 1}
-> FStar.HyperStack.ST.Stack Prims.bool | {
"end_col": 5,
"end_line": 324,
"start_col": 2,
"start_line": 320
} |
FStar.HyperStack.ST.Stack | val recover_x_step_4: tmp:lbuffer uint64 15ul -> Stack bool
(requires fun h -> live h tmp /\
F51.mul_inv_t h (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h (gsub tmp 5ul 5ul))
(ensures fun h0 z h1 -> modifies (loc tmp) h0 h1 /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
F51.fevalh h0 (gsub tmp 5ul 5ul) == F51.fevalh h1 (gsub tmp 5ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
(let u = F51.fevalh h0 (gsub tmp 5ul 5ul) in
let v = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let y = (u `SC.fmul` u) `SC.fsub` v in
(z <==> y == SC.zero))) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let recover_x_step_4 tmp =
let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in
let t0 = sub tmp 10ul 5ul in
fsquare t0 x3; // t0 = x3 * x3
fdifference t0 t0 x2; // t0 - x2
reduce_513 t0;
reduce t0;
is_0 t0 | val recover_x_step_4: tmp:lbuffer uint64 15ul -> Stack bool
(requires fun h -> live h tmp /\
F51.mul_inv_t h (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h (gsub tmp 5ul 5ul))
(ensures fun h0 z h1 -> modifies (loc tmp) h0 h1 /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
F51.fevalh h0 (gsub tmp 5ul 5ul) == F51.fevalh h1 (gsub tmp 5ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
(let u = F51.fevalh h0 (gsub tmp 5ul 5ul) in
let v = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let y = (u `SC.fmul` u) `SC.fsub` v in
(z <==> y == SC.zero)))
let recover_x_step_4 tmp = | true | null | false | let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in
let t0 = sub tmp 10ul 5ul in
fsquare t0 x3;
fdifference t0 t0 x2;
reduce_513 t0;
reduce t0;
is_0 t0 | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint64",
"FStar.UInt32.__uint_to_t",
"Hacl.Impl.Ed25519.RecoverX.is_0",
"Prims.bool",
"Prims.unit",
"Hacl.Bignum25519.reduce",
"Hacl.Bignum25519.reduce_513",
"Hacl.Bignum25519.fdifference",
"Hacl.Bignum25519.fsquare",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Lib.Buffer.sub"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline]
let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL)
inline_for_extraction noextract
val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime)))
let gte_q x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL &&
u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL)
val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1)
[@CInline]
let mul_modp_sqrt_m1 x =
[@inline_let] let (x0, x1, x2, x3, x4) =
(u64 0x00061b274a0ea0b0,
u64 0x0000d5a5fc8f189d,
u64 0x0007ef5e9cbd0c60,
u64 0x00078595a6804c9e,
u64 0x0002b8324804fc1d) in
push_frame();
let sqrt_m1 = create 5ul (u64 0) in
make_u64_5 sqrt_m1 x0 x1 x2 x3 x4;
assert_norm (S51.as_nat5 (x0, x1, x2, x3, x4) == SE.modp_sqrt_m1);
fmul x x sqrt_m1;
pop_frame()
inline_for_extraction noextract
val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2)
let x_mod_2 x =
let x0 = x.(0ul) in
let z = x0 &. u64 1 in
mod_mask_lemma x0 1ul;
Lib.IntTypes.Compatibility.uintv_extensionality (u64 1) (mod_mask #U64 1ul);
z
inline_for_extraction noextract
val recover_x_step_1: x2:elemB -> y:elemB -> Stack unit
(requires fun h ->
live h x2 /\ live h y /\ disjoint x2 y /\
F51.mul_inv_t h y)
(ensures fun h0 _ h1 -> modifies (loc x2) h0 h1 /\
F51.fevalh h1 x2 == F51.as_nat h1 x2 /\
F51.mul_inv_t h1 x2 /\
(let y = F51.fevalh h0 y in
let x2 = F51.fevalh h1 x2 in
let y2 = y `SC.fmul` y in
x2 == (y2 `SC.fsub` SC.one) `SC.fmul` (SC.finv ((SE.d `SC.fmul` y2) `SC.fadd` SC.one))))
let recover_x_step_1 x2 y =
push_frame();
let tmp = create 20ul (u64 0) in
let one = sub tmp 0ul 5ul in
let y2 = sub tmp 5ul 5ul in
let dyyi = sub tmp 10ul 5ul in
let dyy = sub tmp 15ul 5ul in
make_one one;
fsquare y2 y; // y2 = y * y
times_d dyy y2; // dyy = d * y2
fsum dyy dyy one; // dyy = (d * y2) + one
reduce_513 dyy;
inverse dyyi dyy; // dyyi = modp_inv ((d * y2) + one)
fdifference x2 y2 one; // x2 = y2 - one
fmul x2 x2 dyyi; // x2 = (y2 - one) * dyyi
reduce x2;
pop_frame()
inline_for_extraction noextract
val recover_x_step_2: x:elemB -> sign:uint64 -> x2:elemB -> Stack uint8
(requires fun h ->
live h x2 /\ live h x /\ disjoint x x2 /\
F51.mul_inv_t h x /\ F51.fevalh h x2 == F51.as_nat h x2)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
(if F51.fevalh h0 x2 = 0 then (
if v sign = 0 then F51.fevalh h1 x = 0 /\ z == u8 1
else h0 == h1 /\ z == u8 0)
else h0 == h1 /\ z == u8 2))
let recover_x_step_2 x sign x2 =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x2_is_0 = is_0 x2 in
if x2_is_0 then begin
if u64_to_UInt64 sign =^ 0uL then (make_zero x; u8 1) else u8 0 end
else u8 2
inline_for_extraction noextract
val recover_x_step_3: tmp:lbuffer uint64 15ul -> Stack unit
(requires fun h -> live h tmp /\ F51.mul_inv_t h (gsub tmp 0ul 5ul))
(ensures fun h0 _ h1 -> modifies (loc tmp) h0 h1 /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
(let x2 = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let x = x2 `SC.fpow` ((SC.prime + 3) / 8) in
let x = if ((x `SC.fmul` x) `SC.fsub` x2) <> 0 then (x `SC.fmul` SE.modp_sqrt_m1) else x in
F51.fevalh h1 (gsub tmp 5ul 5ul) == x))
let recover_x_step_3 tmp =
let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in // x
let t0 = sub tmp 10ul 5ul in
Hacl.Impl.Ed25519.Pow2_252m2.pow2_252m2 x3 x2; // x3 = x2^((prime + 3) / 8)
fsquare t0 x3; // t0 = x3 * x3
fdifference t0 t0 x2; // t0 = t0 - x2
reduce_513 t0;
reduce t0;
let t0_is_0 = is_0 t0 in
if t0_is_0 then () else mul_modp_sqrt_m1 x3
inline_for_extraction noextract
val recover_x_step_4: tmp:lbuffer uint64 15ul -> Stack bool
(requires fun h -> live h tmp /\
F51.mul_inv_t h (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h (gsub tmp 5ul 5ul))
(ensures fun h0 z h1 -> modifies (loc tmp) h0 h1 /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
F51.fevalh h0 (gsub tmp 5ul 5ul) == F51.fevalh h1 (gsub tmp 5ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
(let u = F51.fevalh h0 (gsub tmp 5ul 5ul) in
let v = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let y = (u `SC.fmul` u) `SC.fsub` v in
(z <==> y == SC.zero))) | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val recover_x_step_4: tmp:lbuffer uint64 15ul -> Stack bool
(requires fun h -> live h tmp /\
F51.mul_inv_t h (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h (gsub tmp 5ul 5ul))
(ensures fun h0 z h1 -> modifies (loc tmp) h0 h1 /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
F51.fevalh h0 (gsub tmp 5ul 5ul) == F51.fevalh h1 (gsub tmp 5ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
(let u = F51.fevalh h0 (gsub tmp 5ul 5ul) in
let v = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let y = (u `SC.fmul` u) `SC.fsub` v in
(z <==> y == SC.zero))) | [] | Hacl.Impl.Ed25519.RecoverX.recover_x_step_4 | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | tmp: Lib.Buffer.lbuffer Lib.IntTypes.uint64 15ul -> FStar.HyperStack.ST.Stack Prims.bool | {
"end_col": 9,
"end_line": 206,
"start_col": 26,
"start_line": 198
} |
FStar.HyperStack.ST.Stack | val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let mul_modp_sqrt_m1 x =
[@inline_let] let (x0, x1, x2, x3, x4) =
(u64 0x00061b274a0ea0b0,
u64 0x0000d5a5fc8f189d,
u64 0x0007ef5e9cbd0c60,
u64 0x00078595a6804c9e,
u64 0x0002b8324804fc1d) in
push_frame();
let sqrt_m1 = create 5ul (u64 0) in
make_u64_5 sqrt_m1 x0 x1 x2 x3 x4;
assert_norm (S51.as_nat5 (x0, x1, x2, x3, x4) == SE.modp_sqrt_m1);
fmul x x sqrt_m1;
pop_frame() | val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1)
let mul_modp_sqrt_m1 x = | true | null | false | [@@ inline_let ]let x0, x1, x2, x3, x4 =
(u64 0x00061b274a0ea0b0,
u64 0x0000d5a5fc8f189d,
u64 0x0007ef5e9cbd0c60,
u64 0x00078595a6804c9e,
u64 0x0002b8324804fc1d)
in
push_frame ();
let sqrt_m1 = create 5ul (u64 0) in
make_u64_5 sqrt_m1 x0 x1 x2 x3 x4;
assert_norm (S51.as_nat5 (x0, x1, x2, x3, x4) == SE.modp_sqrt_m1);
fmul x x sqrt_m1;
pop_frame () | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Hacl.Impl.Ed25519.RecoverX.elemB",
"Lib.IntTypes.uint64",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Hacl.Bignum25519.fmul",
"FStar.Pervasives.assert_norm",
"Prims.eq2",
"Prims.nat",
"Hacl.Spec.Curve25519.Field51.Definition.as_nat5",
"FStar.Pervasives.Native.Mktuple5",
"Spec.Ed25519.PointOps.modp_sqrt_m1",
"Hacl.Bignum25519.make_u64_5",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Lib.Buffer.create",
"FStar.UInt32.__uint_to_t",
"Lib.IntTypes.u64",
"Lib.Buffer.lbuffer",
"FStar.HyperStack.ST.push_frame",
"FStar.Pervasives.Native.tuple5"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline]
let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL)
inline_for_extraction noextract
val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime)))
let gte_q x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL &&
u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL)
val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1)
[@CInline] | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1) | [] | Hacl.Impl.Ed25519.RecoverX.mul_modp_sqrt_m1 | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | x: Hacl.Impl.Ed25519.RecoverX.elemB -> FStar.HyperStack.ST.Stack Prims.unit | {
"end_col": 13,
"end_line": 86,
"start_col": 2,
"start_line": 74
} |
FStar.HyperStack.ST.Stack | val recover_x_step_3: tmp:lbuffer uint64 15ul -> Stack unit
(requires fun h -> live h tmp /\ F51.mul_inv_t h (gsub tmp 0ul 5ul))
(ensures fun h0 _ h1 -> modifies (loc tmp) h0 h1 /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
(let x2 = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let x = x2 `SC.fpow` ((SC.prime + 3) / 8) in
let x = if ((x `SC.fmul` x) `SC.fsub` x2) <> 0 then (x `SC.fmul` SE.modp_sqrt_m1) else x in
F51.fevalh h1 (gsub tmp 5ul 5ul) == x)) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let recover_x_step_3 tmp =
let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in // x
let t0 = sub tmp 10ul 5ul in
Hacl.Impl.Ed25519.Pow2_252m2.pow2_252m2 x3 x2; // x3 = x2^((prime + 3) / 8)
fsquare t0 x3; // t0 = x3 * x3
fdifference t0 t0 x2; // t0 = t0 - x2
reduce_513 t0;
reduce t0;
let t0_is_0 = is_0 t0 in
if t0_is_0 then () else mul_modp_sqrt_m1 x3 | val recover_x_step_3: tmp:lbuffer uint64 15ul -> Stack unit
(requires fun h -> live h tmp /\ F51.mul_inv_t h (gsub tmp 0ul 5ul))
(ensures fun h0 _ h1 -> modifies (loc tmp) h0 h1 /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
(let x2 = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let x = x2 `SC.fpow` ((SC.prime + 3) / 8) in
let x = if ((x `SC.fmul` x) `SC.fsub` x2) <> 0 then (x `SC.fmul` SE.modp_sqrt_m1) else x in
F51.fevalh h1 (gsub tmp 5ul 5ul) == x))
let recover_x_step_3 tmp = | true | null | false | let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in
let t0 = sub tmp 10ul 5ul in
Hacl.Impl.Ed25519.Pow2_252m2.pow2_252m2 x3 x2;
fsquare t0 x3;
fdifference t0 t0 x2;
reduce_513 t0;
reduce t0;
let t0_is_0 = is_0 t0 in
if t0_is_0 then () else mul_modp_sqrt_m1 x3 | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint64",
"FStar.UInt32.__uint_to_t",
"Prims.unit",
"Prims.bool",
"Hacl.Impl.Ed25519.RecoverX.mul_modp_sqrt_m1",
"Hacl.Impl.Ed25519.RecoverX.is_0",
"Hacl.Bignum25519.reduce",
"Hacl.Bignum25519.reduce_513",
"Hacl.Bignum25519.fdifference",
"Hacl.Bignum25519.fsquare",
"Hacl.Impl.Ed25519.Pow2_252m2.pow2_252m2",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Lib.Buffer.sub"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline]
let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL)
inline_for_extraction noextract
val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime)))
let gte_q x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL &&
u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL)
val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1)
[@CInline]
let mul_modp_sqrt_m1 x =
[@inline_let] let (x0, x1, x2, x3, x4) =
(u64 0x00061b274a0ea0b0,
u64 0x0000d5a5fc8f189d,
u64 0x0007ef5e9cbd0c60,
u64 0x00078595a6804c9e,
u64 0x0002b8324804fc1d) in
push_frame();
let sqrt_m1 = create 5ul (u64 0) in
make_u64_5 sqrt_m1 x0 x1 x2 x3 x4;
assert_norm (S51.as_nat5 (x0, x1, x2, x3, x4) == SE.modp_sqrt_m1);
fmul x x sqrt_m1;
pop_frame()
inline_for_extraction noextract
val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2)
let x_mod_2 x =
let x0 = x.(0ul) in
let z = x0 &. u64 1 in
mod_mask_lemma x0 1ul;
Lib.IntTypes.Compatibility.uintv_extensionality (u64 1) (mod_mask #U64 1ul);
z
inline_for_extraction noextract
val recover_x_step_1: x2:elemB -> y:elemB -> Stack unit
(requires fun h ->
live h x2 /\ live h y /\ disjoint x2 y /\
F51.mul_inv_t h y)
(ensures fun h0 _ h1 -> modifies (loc x2) h0 h1 /\
F51.fevalh h1 x2 == F51.as_nat h1 x2 /\
F51.mul_inv_t h1 x2 /\
(let y = F51.fevalh h0 y in
let x2 = F51.fevalh h1 x2 in
let y2 = y `SC.fmul` y in
x2 == (y2 `SC.fsub` SC.one) `SC.fmul` (SC.finv ((SE.d `SC.fmul` y2) `SC.fadd` SC.one))))
let recover_x_step_1 x2 y =
push_frame();
let tmp = create 20ul (u64 0) in
let one = sub tmp 0ul 5ul in
let y2 = sub tmp 5ul 5ul in
let dyyi = sub tmp 10ul 5ul in
let dyy = sub tmp 15ul 5ul in
make_one one;
fsquare y2 y; // y2 = y * y
times_d dyy y2; // dyy = d * y2
fsum dyy dyy one; // dyy = (d * y2) + one
reduce_513 dyy;
inverse dyyi dyy; // dyyi = modp_inv ((d * y2) + one)
fdifference x2 y2 one; // x2 = y2 - one
fmul x2 x2 dyyi; // x2 = (y2 - one) * dyyi
reduce x2;
pop_frame()
inline_for_extraction noextract
val recover_x_step_2: x:elemB -> sign:uint64 -> x2:elemB -> Stack uint8
(requires fun h ->
live h x2 /\ live h x /\ disjoint x x2 /\
F51.mul_inv_t h x /\ F51.fevalh h x2 == F51.as_nat h x2)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
(if F51.fevalh h0 x2 = 0 then (
if v sign = 0 then F51.fevalh h1 x = 0 /\ z == u8 1
else h0 == h1 /\ z == u8 0)
else h0 == h1 /\ z == u8 2))
let recover_x_step_2 x sign x2 =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x2_is_0 = is_0 x2 in
if x2_is_0 then begin
if u64_to_UInt64 sign =^ 0uL then (make_zero x; u8 1) else u8 0 end
else u8 2
inline_for_extraction noextract
val recover_x_step_3: tmp:lbuffer uint64 15ul -> Stack unit
(requires fun h -> live h tmp /\ F51.mul_inv_t h (gsub tmp 0ul 5ul))
(ensures fun h0 _ h1 -> modifies (loc tmp) h0 h1 /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
(let x2 = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let x = x2 `SC.fpow` ((SC.prime + 3) / 8) in
let x = if ((x `SC.fmul` x) `SC.fsub` x2) <> 0 then (x `SC.fmul` SE.modp_sqrt_m1) else x in
F51.fevalh h1 (gsub tmp 5ul 5ul) == x)) | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val recover_x_step_3: tmp:lbuffer uint64 15ul -> Stack unit
(requires fun h -> live h tmp /\ F51.mul_inv_t h (gsub tmp 0ul 5ul))
(ensures fun h0 _ h1 -> modifies (loc tmp) h0 h1 /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
(let x2 = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let x = x2 `SC.fpow` ((SC.prime + 3) / 8) in
let x = if ((x `SC.fmul` x) `SC.fsub` x2) <> 0 then (x `SC.fmul` SE.modp_sqrt_m1) else x in
F51.fevalh h1 (gsub tmp 5ul 5ul) == x)) | [] | Hacl.Impl.Ed25519.RecoverX.recover_x_step_3 | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | tmp: Lib.Buffer.lbuffer Lib.IntTypes.uint64 15ul -> FStar.HyperStack.ST.Stack Prims.unit | {
"end_col": 45,
"end_line": 180,
"start_col": 26,
"start_line": 170
} |
FStar.HyperStack.ST.Stack | val recover_x_:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack bool
(requires fun h ->
live h tmp /\ live h x /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h x /\
F51.felem_fits h y (1, 1, 1, 1, 1)
)
(ensures fun h0 z h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
(z ==> F51.mul_inv_t h1 x) /\
(let res = SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0) in
(Some? res <==> z) /\
(Some? res ==> F51.fevalh h1 x == Some?.v res))
) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let recover_x_ x y sign tmp =
let x2 = sub tmp 0ul 5ul in
let open Lib.RawIntTypes in
let open FStar.UInt8 in
let h0 = ST.get() in
let b = gte_q y in
let res =
if b then false
else (
(**) FStar.Math.Lemmas.small_mod (F51.as_nat h0 y) SC.prime;
recover_x_step_1 x2 y;
let z = recover_x_step_2 x sign x2 in
if (u8_to_UInt8 z =^ 0uy) then false
else if (u8_to_UInt8 z =^ 1uy) then true
else (
recover_x_step_3 tmp;
let z = recover_x_step_4 tmp in
let h1 = ST.get() in
if z = false then false
else (
recover_x_step_5 x y sign tmp;
true)
)
) in
res | val recover_x_:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack bool
(requires fun h ->
live h tmp /\ live h x /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h x /\
F51.felem_fits h y (1, 1, 1, 1, 1)
)
(ensures fun h0 z h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
(z ==> F51.mul_inv_t h1 x) /\
(let res = SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0) in
(Some? res <==> z) /\
(Some? res ==> F51.fevalh h1 x == Some?.v res))
)
let recover_x_ x y sign tmp = | true | null | false | let x2 = sub tmp 0ul 5ul in
let open Lib.RawIntTypes in
let open FStar.UInt8 in
let h0 = ST.get () in
let b = gte_q y in
let res =
if b
then false
else
(FStar.Math.Lemmas.small_mod (F51.as_nat h0 y) SC.prime;
recover_x_step_1 x2 y;
let z = recover_x_step_2 x sign x2 in
if (u8_to_UInt8 z =^ 0uy)
then false
else
if (u8_to_UInt8 z =^ 1uy)
then true
else
(recover_x_step_3 tmp;
let z = recover_x_step_4 tmp in
let h1 = ST.get () in
if z = false
then false
else
(recover_x_step_5 x y sign tmp;
true)))
in
res | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Hacl.Impl.Ed25519.RecoverX.elemB",
"Lib.IntTypes.uint64",
"Prims.l_or",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Lib.IntTypes.v",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"Lib.Buffer.lbuffer",
"FStar.UInt32.__uint_to_t",
"Prims.bool",
"FStar.UInt8.op_Equals_Hat",
"Lib.RawIntTypes.u8_to_UInt8",
"FStar.UInt8.__uint_to_t",
"Prims.unit",
"Hacl.Impl.Ed25519.RecoverX.recover_x_step_5",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Impl.Ed25519.RecoverX.recover_x_step_4",
"Hacl.Impl.Ed25519.RecoverX.recover_x_step_3",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Hacl.Impl.Ed25519.RecoverX.recover_x_step_2",
"Lib.IntTypes.uint8",
"Hacl.Impl.Ed25519.RecoverX.recover_x_step_1",
"FStar.Math.Lemmas.small_mod",
"Hacl.Impl.Ed25519.Field51.as_nat",
"Spec.Curve25519.prime",
"Hacl.Impl.Ed25519.RecoverX.gte_q",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Lib.Buffer.sub"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline]
let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL)
inline_for_extraction noextract
val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime)))
let gte_q x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL &&
u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL)
val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1)
[@CInline]
let mul_modp_sqrt_m1 x =
[@inline_let] let (x0, x1, x2, x3, x4) =
(u64 0x00061b274a0ea0b0,
u64 0x0000d5a5fc8f189d,
u64 0x0007ef5e9cbd0c60,
u64 0x00078595a6804c9e,
u64 0x0002b8324804fc1d) in
push_frame();
let sqrt_m1 = create 5ul (u64 0) in
make_u64_5 sqrt_m1 x0 x1 x2 x3 x4;
assert_norm (S51.as_nat5 (x0, x1, x2, x3, x4) == SE.modp_sqrt_m1);
fmul x x sqrt_m1;
pop_frame()
inline_for_extraction noextract
val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2)
let x_mod_2 x =
let x0 = x.(0ul) in
let z = x0 &. u64 1 in
mod_mask_lemma x0 1ul;
Lib.IntTypes.Compatibility.uintv_extensionality (u64 1) (mod_mask #U64 1ul);
z
inline_for_extraction noextract
val recover_x_step_1: x2:elemB -> y:elemB -> Stack unit
(requires fun h ->
live h x2 /\ live h y /\ disjoint x2 y /\
F51.mul_inv_t h y)
(ensures fun h0 _ h1 -> modifies (loc x2) h0 h1 /\
F51.fevalh h1 x2 == F51.as_nat h1 x2 /\
F51.mul_inv_t h1 x2 /\
(let y = F51.fevalh h0 y in
let x2 = F51.fevalh h1 x2 in
let y2 = y `SC.fmul` y in
x2 == (y2 `SC.fsub` SC.one) `SC.fmul` (SC.finv ((SE.d `SC.fmul` y2) `SC.fadd` SC.one))))
let recover_x_step_1 x2 y =
push_frame();
let tmp = create 20ul (u64 0) in
let one = sub tmp 0ul 5ul in
let y2 = sub tmp 5ul 5ul in
let dyyi = sub tmp 10ul 5ul in
let dyy = sub tmp 15ul 5ul in
make_one one;
fsquare y2 y; // y2 = y * y
times_d dyy y2; // dyy = d * y2
fsum dyy dyy one; // dyy = (d * y2) + one
reduce_513 dyy;
inverse dyyi dyy; // dyyi = modp_inv ((d * y2) + one)
fdifference x2 y2 one; // x2 = y2 - one
fmul x2 x2 dyyi; // x2 = (y2 - one) * dyyi
reduce x2;
pop_frame()
inline_for_extraction noextract
val recover_x_step_2: x:elemB -> sign:uint64 -> x2:elemB -> Stack uint8
(requires fun h ->
live h x2 /\ live h x /\ disjoint x x2 /\
F51.mul_inv_t h x /\ F51.fevalh h x2 == F51.as_nat h x2)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
(if F51.fevalh h0 x2 = 0 then (
if v sign = 0 then F51.fevalh h1 x = 0 /\ z == u8 1
else h0 == h1 /\ z == u8 0)
else h0 == h1 /\ z == u8 2))
let recover_x_step_2 x sign x2 =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x2_is_0 = is_0 x2 in
if x2_is_0 then begin
if u64_to_UInt64 sign =^ 0uL then (make_zero x; u8 1) else u8 0 end
else u8 2
inline_for_extraction noextract
val recover_x_step_3: tmp:lbuffer uint64 15ul -> Stack unit
(requires fun h -> live h tmp /\ F51.mul_inv_t h (gsub tmp 0ul 5ul))
(ensures fun h0 _ h1 -> modifies (loc tmp) h0 h1 /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
(let x2 = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let x = x2 `SC.fpow` ((SC.prime + 3) / 8) in
let x = if ((x `SC.fmul` x) `SC.fsub` x2) <> 0 then (x `SC.fmul` SE.modp_sqrt_m1) else x in
F51.fevalh h1 (gsub tmp 5ul 5ul) == x))
let recover_x_step_3 tmp =
let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in // x
let t0 = sub tmp 10ul 5ul in
Hacl.Impl.Ed25519.Pow2_252m2.pow2_252m2 x3 x2; // x3 = x2^((prime + 3) / 8)
fsquare t0 x3; // t0 = x3 * x3
fdifference t0 t0 x2; // t0 = t0 - x2
reduce_513 t0;
reduce t0;
let t0_is_0 = is_0 t0 in
if t0_is_0 then () else mul_modp_sqrt_m1 x3
inline_for_extraction noextract
val recover_x_step_4: tmp:lbuffer uint64 15ul -> Stack bool
(requires fun h -> live h tmp /\
F51.mul_inv_t h (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h (gsub tmp 5ul 5ul))
(ensures fun h0 z h1 -> modifies (loc tmp) h0 h1 /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
F51.fevalh h0 (gsub tmp 5ul 5ul) == F51.fevalh h1 (gsub tmp 5ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
(let u = F51.fevalh h0 (gsub tmp 5ul 5ul) in
let v = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let y = (u `SC.fmul` u) `SC.fsub` v in
(z <==> y == SC.zero)))
let recover_x_step_4 tmp =
let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in
let t0 = sub tmp 10ul 5ul in
fsquare t0 x3; // t0 = x3 * x3
fdifference t0 t0 x2; // t0 - x2
reduce_513 t0;
reduce t0;
is_0 t0
inline_for_extraction noextract
val recover_x_step_5:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack unit
(requires fun h ->
live h x /\ live h tmp /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h (gsub tmp 5ul 5ul) /\
(let y = F51.as_nat h y in
let sign = (uint_v #U64 sign <> 0) in
let x = F51.fevalh h (gsub tmp 5ul 5ul) in
let x2 = F51.fevalh h (gsub tmp 0ul 5ul) in
((x `SC.fmul` x) `SC.fsub` x2) == SC.zero /\
SE.recover_x y sign ==
(if ((x `SC.fmul` x) `SC.fsub` x2) <> SC.zero then None
else (
let x =
if (x % 2 = 1) <> sign then (SC.prime - x) % SC.prime else x in
Some x))))
(ensures fun h0 _ h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == Some?.v (SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0)))
let recover_x_step_5 x y sign tmp =
let x3 = sub tmp 5ul 5ul in
let t0 = sub tmp 10ul 5ul in
reduce x3;
let x0 = x_mod_2 x3 in
let open Lib.RawIntTypes in
let open FStar.UInt64 in
if not (u64_to_UInt64 x0 =^ u64_to_UInt64 sign) then (
let h0 = ST.get () in
make_zero t0;
fdifference x3 t0 x3; // x3 = (-x) % prime
reduce_513 x3;
reduce x3;
(**) assert_norm (SC.prime % SC.prime = SC.zero % SC.prime);
(**) FStar.Math.Lemmas.mod_add_both SC.prime SC.zero (- (F51.fevalh h0 x3)) SC.prime);
copy x x3
inline_for_extraction noextract
val recover_x_:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack bool
(requires fun h ->
live h tmp /\ live h x /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h x /\
F51.felem_fits h y (1, 1, 1, 1, 1)
)
(ensures fun h0 z h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
(z ==> F51.mul_inv_t h1 x) /\
(let res = SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0) in
(Some? res <==> z) /\
(Some? res ==> F51.fevalh h1 x == Some?.v res))
) | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val recover_x_:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack bool
(requires fun h ->
live h tmp /\ live h x /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h x /\
F51.felem_fits h y (1, 1, 1, 1, 1)
)
(ensures fun h0 z h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
(z ==> F51.mul_inv_t h1 x) /\
(let res = SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0) in
(Some? res <==> z) /\
(Some? res ==> F51.fevalh h1 x == Some?.v res))
) | [] | Hacl.Impl.Ed25519.RecoverX.recover_x_ | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
x: Hacl.Impl.Ed25519.RecoverX.elemB ->
y: Hacl.Impl.Ed25519.RecoverX.elemB ->
sign: Lib.IntTypes.uint64{Lib.IntTypes.v sign = 0 \/ Lib.IntTypes.v sign = 1} ->
tmp: Lib.Buffer.lbuffer Lib.IntTypes.uint64 15ul
-> FStar.HyperStack.ST.Stack Prims.bool | {
"end_col": 6,
"end_line": 299,
"start_col": 29,
"start_line": 274
} |
FStar.HyperStack.ST.Stack | val recover_x_step_2: x:elemB -> sign:uint64 -> x2:elemB -> Stack uint8
(requires fun h ->
live h x2 /\ live h x /\ disjoint x x2 /\
F51.mul_inv_t h x /\ F51.fevalh h x2 == F51.as_nat h x2)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
(if F51.fevalh h0 x2 = 0 then (
if v sign = 0 then F51.fevalh h1 x = 0 /\ z == u8 1
else h0 == h1 /\ z == u8 0)
else h0 == h1 /\ z == u8 2)) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let recover_x_step_2 x sign x2 =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x2_is_0 = is_0 x2 in
if x2_is_0 then begin
if u64_to_UInt64 sign =^ 0uL then (make_zero x; u8 1) else u8 0 end
else u8 2 | val recover_x_step_2: x:elemB -> sign:uint64 -> x2:elemB -> Stack uint8
(requires fun h ->
live h x2 /\ live h x /\ disjoint x x2 /\
F51.mul_inv_t h x /\ F51.fevalh h x2 == F51.as_nat h x2)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
(if F51.fevalh h0 x2 = 0 then (
if v sign = 0 then F51.fevalh h1 x = 0 /\ z == u8 1
else h0 == h1 /\ z == u8 0)
else h0 == h1 /\ z == u8 2))
let recover_x_step_2 x sign x2 = | true | null | false | let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x2_is_0 = is_0 x2 in
if x2_is_0
then
if u64_to_UInt64 sign =^ 0uL
then
(make_zero x;
u8 1)
else u8 0
else u8 2 | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Hacl.Impl.Ed25519.RecoverX.elemB",
"Lib.IntTypes.uint64",
"FStar.UInt64.op_Equals_Hat",
"Lib.RawIntTypes.u64_to_UInt64",
"FStar.UInt64.__uint_to_t",
"Lib.IntTypes.u8",
"Lib.IntTypes.uint8",
"Prims.unit",
"Hacl.Bignum25519.make_zero",
"Prims.bool",
"Hacl.Impl.Ed25519.RecoverX.is_0"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline]
let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL)
inline_for_extraction noextract
val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime)))
let gte_q x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL &&
u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL)
val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1)
[@CInline]
let mul_modp_sqrt_m1 x =
[@inline_let] let (x0, x1, x2, x3, x4) =
(u64 0x00061b274a0ea0b0,
u64 0x0000d5a5fc8f189d,
u64 0x0007ef5e9cbd0c60,
u64 0x00078595a6804c9e,
u64 0x0002b8324804fc1d) in
push_frame();
let sqrt_m1 = create 5ul (u64 0) in
make_u64_5 sqrt_m1 x0 x1 x2 x3 x4;
assert_norm (S51.as_nat5 (x0, x1, x2, x3, x4) == SE.modp_sqrt_m1);
fmul x x sqrt_m1;
pop_frame()
inline_for_extraction noextract
val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2)
let x_mod_2 x =
let x0 = x.(0ul) in
let z = x0 &. u64 1 in
mod_mask_lemma x0 1ul;
Lib.IntTypes.Compatibility.uintv_extensionality (u64 1) (mod_mask #U64 1ul);
z
inline_for_extraction noextract
val recover_x_step_1: x2:elemB -> y:elemB -> Stack unit
(requires fun h ->
live h x2 /\ live h y /\ disjoint x2 y /\
F51.mul_inv_t h y)
(ensures fun h0 _ h1 -> modifies (loc x2) h0 h1 /\
F51.fevalh h1 x2 == F51.as_nat h1 x2 /\
F51.mul_inv_t h1 x2 /\
(let y = F51.fevalh h0 y in
let x2 = F51.fevalh h1 x2 in
let y2 = y `SC.fmul` y in
x2 == (y2 `SC.fsub` SC.one) `SC.fmul` (SC.finv ((SE.d `SC.fmul` y2) `SC.fadd` SC.one))))
let recover_x_step_1 x2 y =
push_frame();
let tmp = create 20ul (u64 0) in
let one = sub tmp 0ul 5ul in
let y2 = sub tmp 5ul 5ul in
let dyyi = sub tmp 10ul 5ul in
let dyy = sub tmp 15ul 5ul in
make_one one;
fsquare y2 y; // y2 = y * y
times_d dyy y2; // dyy = d * y2
fsum dyy dyy one; // dyy = (d * y2) + one
reduce_513 dyy;
inverse dyyi dyy; // dyyi = modp_inv ((d * y2) + one)
fdifference x2 y2 one; // x2 = y2 - one
fmul x2 x2 dyyi; // x2 = (y2 - one) * dyyi
reduce x2;
pop_frame()
inline_for_extraction noextract
val recover_x_step_2: x:elemB -> sign:uint64 -> x2:elemB -> Stack uint8
(requires fun h ->
live h x2 /\ live h x /\ disjoint x x2 /\
F51.mul_inv_t h x /\ F51.fevalh h x2 == F51.as_nat h x2)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
(if F51.fevalh h0 x2 = 0 then (
if v sign = 0 then F51.fevalh h1 x = 0 /\ z == u8 1
else h0 == h1 /\ z == u8 0)
else h0 == h1 /\ z == u8 2)) | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val recover_x_step_2: x:elemB -> sign:uint64 -> x2:elemB -> Stack uint8
(requires fun h ->
live h x2 /\ live h x /\ disjoint x x2 /\
F51.mul_inv_t h x /\ F51.fevalh h x2 == F51.as_nat h x2)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
(if F51.fevalh h0 x2 = 0 then (
if v sign = 0 then F51.fevalh h1 x = 0 /\ z == u8 1
else h0 == h1 /\ z == u8 0)
else h0 == h1 /\ z == u8 2)) | [] | Hacl.Impl.Ed25519.RecoverX.recover_x_step_2 | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
x: Hacl.Impl.Ed25519.RecoverX.elemB ->
sign: Lib.IntTypes.uint64 ->
x2: Hacl.Impl.Ed25519.RecoverX.elemB
-> FStar.HyperStack.ST.Stack Lib.IntTypes.uint8 | {
"end_col": 11,
"end_line": 155,
"start_col": 2,
"start_line": 150
} |
FStar.HyperStack.ST.Stack | val recover_x_step_1: x2:elemB -> y:elemB -> Stack unit
(requires fun h ->
live h x2 /\ live h y /\ disjoint x2 y /\
F51.mul_inv_t h y)
(ensures fun h0 _ h1 -> modifies (loc x2) h0 h1 /\
F51.fevalh h1 x2 == F51.as_nat h1 x2 /\
F51.mul_inv_t h1 x2 /\
(let y = F51.fevalh h0 y in
let x2 = F51.fevalh h1 x2 in
let y2 = y `SC.fmul` y in
x2 == (y2 `SC.fsub` SC.one) `SC.fmul` (SC.finv ((SE.d `SC.fmul` y2) `SC.fadd` SC.one)))) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let recover_x_step_1 x2 y =
push_frame();
let tmp = create 20ul (u64 0) in
let one = sub tmp 0ul 5ul in
let y2 = sub tmp 5ul 5ul in
let dyyi = sub tmp 10ul 5ul in
let dyy = sub tmp 15ul 5ul in
make_one one;
fsquare y2 y; // y2 = y * y
times_d dyy y2; // dyy = d * y2
fsum dyy dyy one; // dyy = (d * y2) + one
reduce_513 dyy;
inverse dyyi dyy; // dyyi = modp_inv ((d * y2) + one)
fdifference x2 y2 one; // x2 = y2 - one
fmul x2 x2 dyyi; // x2 = (y2 - one) * dyyi
reduce x2;
pop_frame() | val recover_x_step_1: x2:elemB -> y:elemB -> Stack unit
(requires fun h ->
live h x2 /\ live h y /\ disjoint x2 y /\
F51.mul_inv_t h y)
(ensures fun h0 _ h1 -> modifies (loc x2) h0 h1 /\
F51.fevalh h1 x2 == F51.as_nat h1 x2 /\
F51.mul_inv_t h1 x2 /\
(let y = F51.fevalh h0 y in
let x2 = F51.fevalh h1 x2 in
let y2 = y `SC.fmul` y in
x2 == (y2 `SC.fsub` SC.one) `SC.fmul` (SC.finv ((SE.d `SC.fmul` y2) `SC.fadd` SC.one))))
let recover_x_step_1 x2 y = | true | null | false | push_frame ();
let tmp = create 20ul (u64 0) in
let one = sub tmp 0ul 5ul in
let y2 = sub tmp 5ul 5ul in
let dyyi = sub tmp 10ul 5ul in
let dyy = sub tmp 15ul 5ul in
make_one one;
fsquare y2 y;
times_d dyy y2;
fsum dyy dyy one;
reduce_513 dyy;
inverse dyyi dyy;
fdifference x2 y2 one;
fmul x2 x2 dyyi;
reduce x2;
pop_frame () | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Hacl.Impl.Ed25519.RecoverX.elemB",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Hacl.Bignum25519.reduce",
"Hacl.Bignum25519.fmul",
"Hacl.Bignum25519.fdifference",
"Hacl.Bignum25519.inverse",
"Hacl.Bignum25519.reduce_513",
"Hacl.Bignum25519.fsum",
"Hacl.Bignum25519.times_d",
"Hacl.Bignum25519.fsquare",
"Hacl.Bignum25519.make_one",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Lib.Buffer.sub",
"Lib.IntTypes.uint64",
"FStar.UInt32.__uint_to_t",
"Lib.Buffer.create",
"Lib.IntTypes.u64",
"Lib.Buffer.lbuffer",
"FStar.HyperStack.ST.push_frame"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline]
let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL)
inline_for_extraction noextract
val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime)))
let gte_q x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL &&
u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL)
val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1)
[@CInline]
let mul_modp_sqrt_m1 x =
[@inline_let] let (x0, x1, x2, x3, x4) =
(u64 0x00061b274a0ea0b0,
u64 0x0000d5a5fc8f189d,
u64 0x0007ef5e9cbd0c60,
u64 0x00078595a6804c9e,
u64 0x0002b8324804fc1d) in
push_frame();
let sqrt_m1 = create 5ul (u64 0) in
make_u64_5 sqrt_m1 x0 x1 x2 x3 x4;
assert_norm (S51.as_nat5 (x0, x1, x2, x3, x4) == SE.modp_sqrt_m1);
fmul x x sqrt_m1;
pop_frame()
inline_for_extraction noextract
val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2)
let x_mod_2 x =
let x0 = x.(0ul) in
let z = x0 &. u64 1 in
mod_mask_lemma x0 1ul;
Lib.IntTypes.Compatibility.uintv_extensionality (u64 1) (mod_mask #U64 1ul);
z
inline_for_extraction noextract
val recover_x_step_1: x2:elemB -> y:elemB -> Stack unit
(requires fun h ->
live h x2 /\ live h y /\ disjoint x2 y /\
F51.mul_inv_t h y)
(ensures fun h0 _ h1 -> modifies (loc x2) h0 h1 /\
F51.fevalh h1 x2 == F51.as_nat h1 x2 /\
F51.mul_inv_t h1 x2 /\
(let y = F51.fevalh h0 y in
let x2 = F51.fevalh h1 x2 in
let y2 = y `SC.fmul` y in
x2 == (y2 `SC.fsub` SC.one) `SC.fmul` (SC.finv ((SE.d `SC.fmul` y2) `SC.fadd` SC.one)))) | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val recover_x_step_1: x2:elemB -> y:elemB -> Stack unit
(requires fun h ->
live h x2 /\ live h y /\ disjoint x2 y /\
F51.mul_inv_t h y)
(ensures fun h0 _ h1 -> modifies (loc x2) h0 h1 /\
F51.fevalh h1 x2 == F51.as_nat h1 x2 /\
F51.mul_inv_t h1 x2 /\
(let y = F51.fevalh h0 y in
let x2 = F51.fevalh h1 x2 in
let y2 = y `SC.fmul` y in
x2 == (y2 `SC.fsub` SC.one) `SC.fmul` (SC.finv ((SE.d `SC.fmul` y2) `SC.fadd` SC.one)))) | [] | Hacl.Impl.Ed25519.RecoverX.recover_x_step_1 | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | x2: Hacl.Impl.Ed25519.RecoverX.elemB -> y: Hacl.Impl.Ed25519.RecoverX.elemB
-> FStar.HyperStack.ST.Stack Prims.unit | {
"end_col": 13,
"end_line": 134,
"start_col": 2,
"start_line": 117
} |
FStar.HyperStack.ST.Stack | val recover_x_step_5:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack unit
(requires fun h ->
live h x /\ live h tmp /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h (gsub tmp 5ul 5ul) /\
(let y = F51.as_nat h y in
let sign = (uint_v #U64 sign <> 0) in
let x = F51.fevalh h (gsub tmp 5ul 5ul) in
let x2 = F51.fevalh h (gsub tmp 0ul 5ul) in
((x `SC.fmul` x) `SC.fsub` x2) == SC.zero /\
SE.recover_x y sign ==
(if ((x `SC.fmul` x) `SC.fsub` x2) <> SC.zero then None
else (
let x =
if (x % 2 = 1) <> sign then (SC.prime - x) % SC.prime else x in
Some x))))
(ensures fun h0 _ h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == Some?.v (SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0))) | [
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Curve25519.Field51.Definition",
"short_module": "S51"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum25519",
"short_module": null
},
{
"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.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Ed25519",
"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
}
] | false | let recover_x_step_5 x y sign tmp =
let x3 = sub tmp 5ul 5ul in
let t0 = sub tmp 10ul 5ul in
reduce x3;
let x0 = x_mod_2 x3 in
let open Lib.RawIntTypes in
let open FStar.UInt64 in
if not (u64_to_UInt64 x0 =^ u64_to_UInt64 sign) then (
let h0 = ST.get () in
make_zero t0;
fdifference x3 t0 x3; // x3 = (-x) % prime
reduce_513 x3;
reduce x3;
(**) assert_norm (SC.prime % SC.prime = SC.zero % SC.prime);
(**) FStar.Math.Lemmas.mod_add_both SC.prime SC.zero (- (F51.fevalh h0 x3)) SC.prime);
copy x x3 | val recover_x_step_5:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack unit
(requires fun h ->
live h x /\ live h tmp /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h (gsub tmp 5ul 5ul) /\
(let y = F51.as_nat h y in
let sign = (uint_v #U64 sign <> 0) in
let x = F51.fevalh h (gsub tmp 5ul 5ul) in
let x2 = F51.fevalh h (gsub tmp 0ul 5ul) in
((x `SC.fmul` x) `SC.fsub` x2) == SC.zero /\
SE.recover_x y sign ==
(if ((x `SC.fmul` x) `SC.fsub` x2) <> SC.zero then None
else (
let x =
if (x % 2 = 1) <> sign then (SC.prime - x) % SC.prime else x in
Some x))))
(ensures fun h0 _ h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == Some?.v (SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0)))
let recover_x_step_5 x y sign tmp = | true | null | false | let x3 = sub tmp 5ul 5ul in
let t0 = sub tmp 10ul 5ul in
reduce x3;
let x0 = x_mod_2 x3 in
let open Lib.RawIntTypes in
let open FStar.UInt64 in
if not (u64_to_UInt64 x0 =^ u64_to_UInt64 sign)
then
(let h0 = ST.get () in
make_zero t0;
fdifference x3 t0 x3;
reduce_513 x3;
reduce x3;
assert_norm (SC.prime % SC.prime = SC.zero % SC.prime);
FStar.Math.Lemmas.mod_add_both SC.prime SC.zero (- (F51.fevalh h0 x3)) SC.prime);
copy x x3 | {
"checked_file": "Hacl.Impl.Ed25519.RecoverX.fst.checked",
"dependencies": [
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.IntTypes.Compatibility.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Curve25519.Field51.Definition.fst.checked",
"Hacl.Impl.Ed25519.Pow2_252m2.fst.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.All.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Ed25519.RecoverX.fst"
} | [] | [
"Hacl.Impl.Ed25519.RecoverX.elemB",
"Lib.IntTypes.uint64",
"Prims.l_or",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Lib.IntTypes.v",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"Lib.Buffer.lbuffer",
"FStar.UInt32.__uint_to_t",
"Lib.Buffer.copy",
"Lib.Buffer.MUT",
"Prims.unit",
"Prims.op_Negation",
"FStar.UInt64.op_Equals_Hat",
"Lib.RawIntTypes.u64_to_UInt64",
"FStar.Math.Lemmas.mod_add_both",
"Spec.Curve25519.prime",
"Spec.Curve25519.zero",
"Prims.op_Minus",
"Hacl.Impl.Ed25519.Field51.fevalh",
"FStar.Pervasives.assert_norm",
"Prims.op_Modulus",
"Hacl.Bignum25519.reduce",
"Hacl.Bignum25519.reduce_513",
"Hacl.Bignum25519.fdifference",
"Hacl.Bignum25519.make_zero",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Prims.bool",
"Lib.IntTypes.int_t",
"Hacl.Impl.Ed25519.RecoverX.x_mod_2",
"Lib.Buffer.lbuffer_t",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Lib.Buffer.sub"
] | [] | module Hacl.Impl.Ed25519.RecoverX
module ST = FStar.HyperStack.ST
open FStar.HyperStack.All
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum25519
module F51 = Hacl.Impl.Ed25519.Field51
module S51 = Hacl.Spec.Curve25519.Field51.Definition
module SC = Spec.Curve25519
module SE = Spec.Ed25519
#reset-options "--z3rlimit 50 --max_fuel 0 --max_ifuel 0"
inline_for_extraction noextract
let elemB = lbuffer uint64 5ul
val is_0: x:elemB -> Stack bool
(requires fun h -> live h x /\ F51.as_nat h x == F51.fevalh h x)
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.fevalh h0 x == SC.zero)))
[@CInline]
let is_0 x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 =^ 0uL &&
u64_to_UInt64 x1 =^ 0uL &&
u64_to_UInt64 x2 =^ 0uL &&
u64_to_UInt64 x3 =^ 0uL &&
u64_to_UInt64 x4 =^ 0uL)
inline_for_extraction noextract
val gte_q: x:elemB -> Stack bool
(requires fun h -> live h x /\
F51.felem_fits h x (1, 1, 1, 1, 1))
(ensures fun h0 b h1 -> h0 == h1 /\
(b <==> (F51.as_nat h0 x >= SC.prime)))
let gte_q x =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x0 = x.(0ul) in
let x1 = x.(1ul) in
let x2 = x.(2ul) in
let x3 = x.(3ul) in
let x4 = x.(4ul) in
(u64_to_UInt64 x0 >=^ 0x7ffffffffffeduL &&
u64_to_UInt64 x1 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x2 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x3 =^ 0x7ffffffffffffuL &&
u64_to_UInt64 x4 =^ 0x7ffffffffffffuL)
val mul_modp_sqrt_m1: x:elemB -> Stack unit
(requires fun h -> live h x /\ F51.mul_inv_t h x)
(ensures fun h0 _ h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == F51.fevalh h0 x `SC.fmul` SE.modp_sqrt_m1)
[@CInline]
let mul_modp_sqrt_m1 x =
[@inline_let] let (x0, x1, x2, x3, x4) =
(u64 0x00061b274a0ea0b0,
u64 0x0000d5a5fc8f189d,
u64 0x0007ef5e9cbd0c60,
u64 0x00078595a6804c9e,
u64 0x0002b8324804fc1d) in
push_frame();
let sqrt_m1 = create 5ul (u64 0) in
make_u64_5 sqrt_m1 x0 x1 x2 x3 x4;
assert_norm (S51.as_nat5 (x0, x1, x2, x3, x4) == SE.modp_sqrt_m1);
fmul x x sqrt_m1;
pop_frame()
inline_for_extraction noextract
val x_mod_2: x:felem -> Stack uint64
(requires fun h -> live h x)
(ensures fun h0 z h1 -> h0 == h1 /\ v z < 2 /\
v z == F51.as_nat h0 x % 2)
let x_mod_2 x =
let x0 = x.(0ul) in
let z = x0 &. u64 1 in
mod_mask_lemma x0 1ul;
Lib.IntTypes.Compatibility.uintv_extensionality (u64 1) (mod_mask #U64 1ul);
z
inline_for_extraction noextract
val recover_x_step_1: x2:elemB -> y:elemB -> Stack unit
(requires fun h ->
live h x2 /\ live h y /\ disjoint x2 y /\
F51.mul_inv_t h y)
(ensures fun h0 _ h1 -> modifies (loc x2) h0 h1 /\
F51.fevalh h1 x2 == F51.as_nat h1 x2 /\
F51.mul_inv_t h1 x2 /\
(let y = F51.fevalh h0 y in
let x2 = F51.fevalh h1 x2 in
let y2 = y `SC.fmul` y in
x2 == (y2 `SC.fsub` SC.one) `SC.fmul` (SC.finv ((SE.d `SC.fmul` y2) `SC.fadd` SC.one))))
let recover_x_step_1 x2 y =
push_frame();
let tmp = create 20ul (u64 0) in
let one = sub tmp 0ul 5ul in
let y2 = sub tmp 5ul 5ul in
let dyyi = sub tmp 10ul 5ul in
let dyy = sub tmp 15ul 5ul in
make_one one;
fsquare y2 y; // y2 = y * y
times_d dyy y2; // dyy = d * y2
fsum dyy dyy one; // dyy = (d * y2) + one
reduce_513 dyy;
inverse dyyi dyy; // dyyi = modp_inv ((d * y2) + one)
fdifference x2 y2 one; // x2 = y2 - one
fmul x2 x2 dyyi; // x2 = (y2 - one) * dyyi
reduce x2;
pop_frame()
inline_for_extraction noextract
val recover_x_step_2: x:elemB -> sign:uint64 -> x2:elemB -> Stack uint8
(requires fun h ->
live h x2 /\ live h x /\ disjoint x x2 /\
F51.mul_inv_t h x /\ F51.fevalh h x2 == F51.as_nat h x2)
(ensures fun h0 z h1 -> modifies (loc x) h0 h1 /\
F51.mul_inv_t h1 x /\
(if F51.fevalh h0 x2 = 0 then (
if v sign = 0 then F51.fevalh h1 x = 0 /\ z == u8 1
else h0 == h1 /\ z == u8 0)
else h0 == h1 /\ z == u8 2))
let recover_x_step_2 x sign x2 =
let open Lib.RawIntTypes in
let open FStar.UInt64 in
let x2_is_0 = is_0 x2 in
if x2_is_0 then begin
if u64_to_UInt64 sign =^ 0uL then (make_zero x; u8 1) else u8 0 end
else u8 2
inline_for_extraction noextract
val recover_x_step_3: tmp:lbuffer uint64 15ul -> Stack unit
(requires fun h -> live h tmp /\ F51.mul_inv_t h (gsub tmp 0ul 5ul))
(ensures fun h0 _ h1 -> modifies (loc tmp) h0 h1 /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
(let x2 = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let x = x2 `SC.fpow` ((SC.prime + 3) / 8) in
let x = if ((x `SC.fmul` x) `SC.fsub` x2) <> 0 then (x `SC.fmul` SE.modp_sqrt_m1) else x in
F51.fevalh h1 (gsub tmp 5ul 5ul) == x))
let recover_x_step_3 tmp =
let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in // x
let t0 = sub tmp 10ul 5ul in
Hacl.Impl.Ed25519.Pow2_252m2.pow2_252m2 x3 x2; // x3 = x2^((prime + 3) / 8)
fsquare t0 x3; // t0 = x3 * x3
fdifference t0 t0 x2; // t0 = t0 - x2
reduce_513 t0;
reduce t0;
let t0_is_0 = is_0 t0 in
if t0_is_0 then () else mul_modp_sqrt_m1 x3
inline_for_extraction noextract
val recover_x_step_4: tmp:lbuffer uint64 15ul -> Stack bool
(requires fun h -> live h tmp /\
F51.mul_inv_t h (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h (gsub tmp 5ul 5ul))
(ensures fun h0 z h1 -> modifies (loc tmp) h0 h1 /\
F51.fevalh h0 (gsub tmp 0ul 5ul) == F51.fevalh h1 (gsub tmp 0ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 0ul 5ul) /\
F51.fevalh h0 (gsub tmp 5ul 5ul) == F51.fevalh h1 (gsub tmp 5ul 5ul) /\
F51.mul_inv_t h1 (gsub tmp 5ul 5ul) /\
(let u = F51.fevalh h0 (gsub tmp 5ul 5ul) in
let v = F51.fevalh h0 (gsub tmp 0ul 5ul) in
let y = (u `SC.fmul` u) `SC.fsub` v in
(z <==> y == SC.zero)))
let recover_x_step_4 tmp =
let x2 = sub tmp 0ul 5ul in
let x3 = sub tmp 5ul 5ul in
let t0 = sub tmp 10ul 5ul in
fsquare t0 x3; // t0 = x3 * x3
fdifference t0 t0 x2; // t0 - x2
reduce_513 t0;
reduce t0;
is_0 t0
inline_for_extraction noextract
val recover_x_step_5:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack unit
(requires fun h ->
live h x /\ live h tmp /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h (gsub tmp 5ul 5ul) /\
(let y = F51.as_nat h y in
let sign = (uint_v #U64 sign <> 0) in
let x = F51.fevalh h (gsub tmp 5ul 5ul) in
let x2 = F51.fevalh h (gsub tmp 0ul 5ul) in
((x `SC.fmul` x) `SC.fsub` x2) == SC.zero /\
SE.recover_x y sign ==
(if ((x `SC.fmul` x) `SC.fsub` x2) <> SC.zero then None
else (
let x =
if (x % 2 = 1) <> sign then (SC.prime - x) % SC.prime else x in
Some x))))
(ensures fun h0 _ h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == Some?.v (SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0))) | false | false | Hacl.Impl.Ed25519.RecoverX.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"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"
} | null | val recover_x_step_5:
x:elemB
-> y:elemB
-> sign:uint64{v sign = 0 \/ v sign = 1}
-> tmp:lbuffer uint64 15ul ->
Stack unit
(requires fun h ->
live h x /\ live h tmp /\ live h y /\
disjoint x y /\ disjoint tmp x /\ disjoint tmp y /\
F51.mul_inv_t h (gsub tmp 5ul 5ul) /\
(let y = F51.as_nat h y in
let sign = (uint_v #U64 sign <> 0) in
let x = F51.fevalh h (gsub tmp 5ul 5ul) in
let x2 = F51.fevalh h (gsub tmp 0ul 5ul) in
((x `SC.fmul` x) `SC.fsub` x2) == SC.zero /\
SE.recover_x y sign ==
(if ((x `SC.fmul` x) `SC.fsub` x2) <> SC.zero then None
else (
let x =
if (x % 2 = 1) <> sign then (SC.prime - x) % SC.prime else x in
Some x))))
(ensures fun h0 _ h1 -> modifies (loc x |+| loc tmp) h0 h1 /\
F51.mul_inv_t h1 x /\
F51.fevalh h1 x == Some?.v (SE.recover_x (F51.as_nat h0 y) (uint_v #U64 sign <> 0))) | [] | Hacl.Impl.Ed25519.RecoverX.recover_x_step_5 | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
x: Hacl.Impl.Ed25519.RecoverX.elemB ->
y: Hacl.Impl.Ed25519.RecoverX.elemB ->
sign: Lib.IntTypes.uint64{Lib.IntTypes.v sign = 0 \/ Lib.IntTypes.v sign = 1} ->
tmp: Lib.Buffer.lbuffer Lib.IntTypes.uint64 15ul
-> FStar.HyperStack.ST.Stack Prims.unit | {
"end_col": 11,
"end_line": 251,
"start_col": 35,
"start_line": 235
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.All",
"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
}
] | false | let dummy_pos = {
filename="";
line=0;
col=0;
} | let dummy_pos = | false | null | false | { filename = ""; line = 0; col = 0 } | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.Mkpos"
] | [] | (*
Copyright 2019 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 Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q) | false | true | Ast.fst | {
"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"
} | null | val dummy_pos : Ast.pos | [] | Ast.dummy_pos | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Ast.pos | {
"end_col": 8,
"end_line": 97,
"start_col": 2,
"start_line": 95
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.All",
"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
}
] | false | let reserved_prefix = "___" | let reserved_prefix = | false | null | false | "___" | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [] | [] | (*
Copyright 2019 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 Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All | false | true | Ast.fst | {
"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"
} | null | val reserved_prefix : Prims.string | [] | Ast.reserved_prefix | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Prims.string | {
"end_col": 27,
"end_line": 21,
"start_col": 22,
"start_line": 21
} |
|
Prims.Tot | val with_meta_t_to_yojson (f: ('a -> 'b)) (x: with_meta_t 'a) : 'b | [
{
"abbrev": false,
"full_module": "FStar.All",
"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
}
] | false | let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v | val with_meta_t_to_yojson (f: ('a -> 'b)) (x: with_meta_t 'a) : 'b
let with_meta_t_to_yojson (f: ('a -> 'b)) (x: with_meta_t 'a) : 'b = | false | null | false | f x.v | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.with_meta_t",
"Ast.__proj__Mkwith_meta_t__item__v"
] | [] | (*
Copyright 2019 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 Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a) | false | false | Ast.fst | {
"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"
} | null | val with_meta_t_to_yojson (f: ('a -> 'b)) (x: with_meta_t 'a) : 'b | [] | Ast.with_meta_t_to_yojson | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | f: (_: 'a -> 'b) -> x: Ast.with_meta_t 'a -> 'b | {
"end_col": 9,
"end_line": 113,
"start_col": 4,
"start_line": 113
} |
Prims.Tot | val with_range (x: 'a) (r: range) : with_meta_t 'a | [
{
"abbrev": false,
"full_module": "FStar.All",
"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
}
] | false | let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r [] | val with_range (x: 'a) (r: range) : with_meta_t 'a
let with_range (x: 'a) (r: range) : with_meta_t 'a = | false | null | false | with_range_and_comments x r [] | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.range",
"Ast.with_range_and_comments",
"Prims.Nil",
"Prims.string",
"Ast.with_meta_t"
] | [] | (*
Copyright 2019 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 Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c | false | false | Ast.fst | {
"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"
} | null | val with_range (x: 'a) (r: range) : with_meta_t 'a | [] | Ast.with_range | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: 'a -> r: Ast.range -> Ast.with_meta_t 'a | {
"end_col": 81,
"end_line": 119,
"start_col": 51,
"start_line": 119
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.All",
"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
}
] | false | let ident_name i = i.v.name | let ident_name i = | false | null | false | i.v.name | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.with_meta_t",
"Ast.ident'",
"Ast.__proj__Mkident'__item__name",
"Ast.__proj__Mkwith_meta_t__item__v",
"Prims.string"
] | [] | (*
Copyright 2019 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 Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name | false | true | Ast.fst | {
"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"
} | null | val ident_name : i: Ast.with_meta_t Ast.ident' -> Prims.string | [] | Ast.ident_name | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | i: Ast.with_meta_t Ast.ident' -> Prims.string | {
"end_col": 27,
"end_line": 135,
"start_col": 19,
"start_line": 135
} |
|
Prims.Tot | val decl_has_out_expr (d: decl) : Tot bool | [
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"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
}
] | false | let decl_has_out_expr (d: decl) : Tot bool =
match d.d_decl.v with
| Record _ _ _ ast_fields ->
record_has_out_expr ast_fields
| CaseType _ _ switch_case ->
switch_case_has_out_expr switch_case
| _ -> false | val decl_has_out_expr (d: decl) : Tot bool
let decl_has_out_expr (d: decl) : Tot bool = | false | null | false | match d.d_decl.v with
| Record _ _ _ ast_fields -> record_has_out_expr ast_fields
| CaseType _ _ switch_case -> switch_case_has_out_expr switch_case
| _ -> false | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.decl",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.decl'",
"Ast.__proj__Mkdecl__item__d_decl",
"Ast.typedef_names",
"Prims.list",
"Ast.param",
"FStar.Pervasives.Native.option",
"Ast.expr",
"Ast.record",
"Ast.record_has_out_expr",
"Ast.switch_case",
"Ast.switch_case_has_out_expr",
"Prims.bool"
] | [] | (*
Copyright 2019 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 Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ'
let field_typ = t:typ { Type_app? t.v }
[@@ PpxDerivingYoJson ]
noeq
type atomic_action =
| Action_return of expr
| Action_abort
| Action_field_pos_64
| Action_field_pos_32
| Action_field_ptr
| Action_field_ptr_after: sz:expr -> write_to:out_expr -> atomic_action
| Action_deref of ident
| Action_assignment : lhs:out_expr -> rhs:expr -> atomic_action
| Action_call : f:ident -> args:list expr -> atomic_action
noeq
[@@ PpxDerivingYoJson ]
type action' =
| Atomic_action of atomic_action
| Action_seq : hd:atomic_action -> tl:action -> action'
| Action_ite : hd:expr -> then_:action -> else_:option action -> action'
| Action_let : i:ident -> a:atomic_action -> k:action -> action'
| Action_act : action -> action'
and action = with_meta_t action'
open FStar.List.Tot
let sequence_non_failing_actions (a0:action{Action_act? a0.v}) (a1:action {Action_act? a1.v})
: a:action{Action_act? a.v}
= let rec seq (a0:action) =
let w a =
with_range_and_comments a
a1.range
(a0.comments @ a1.comments)
in
let Action_act a1 = a1.v in
match a0.v with
| Atomic_action a ->
w (Action_seq a a1)
| Action_seq a0 tl ->
w (Action_seq a0 (seq tl))
| Action_ite hd th el ->
let th = seq th in
let el =
match el with
| None -> Some a1
| Some el -> Some (seq el)
in
w (Action_ite hd th el)
| Action_let i a k ->
w (Action_let i a (seq k))
| Action_act a ->
seq a
in
let res = seq a0 in
with_range_and_comments (Action_act res)
res.range
res.comments
[@@ PpxDerivingYoJson ]
type qualifier =
| Immutable
| Mutable
/// Parameters: Type definitions can be parameterized by values
/// Parameters have a name and are always annoted with their type
[@@ PpxDerivingYoJson ]
type param = typ & ident & qualifier
[@@ PpxDerivingYoJson ]
noeq
type bitfield_attr' = {
bitfield_width : int;
bitfield_identifier : int;
bitfield_type : typ;
bitfield_from : int;
bitfield_to: int
}
and bitfield_attr = with_meta_t bitfield_attr'
[@@ PpxDerivingYoJson ]
let field_bitwidth_t = either (with_meta_t int) bitfield_attr
[@@ PpxDerivingYoJson ]
type array_qualifier =
| ByteArrayByteSize //[
| ArrayByteSize //[:byte-size
| ArrayByteSizeAtMost //[:byte-size-at-most
| ArrayByteSizeSingleElementArray //[:byte-size-single-element-array
[@@ PpxDerivingYoJson ]
noeq
type field_array_t =
| FieldScalar
| FieldArrayQualified of (expr & array_qualifier) //array size in bytes, the qualifier indicates whether this is a variable-length suffix or not
| FieldString of (option expr)
[@@ PpxDerivingYoJson ]
noeq
type atomic_field' = {
field_dependence:bool; //computed; whether or not the rest of the struct depends on this field
field_ident:ident; //name of the field
field_type:typ; //type of the field
field_array_opt: field_array_t;
field_constraint:option expr; //refinement constraint
field_bitwidth:option field_bitwidth_t; //bits used for the field; elaborate from Inl to Inr
field_action:option (action & bool); //boo indicates if the action depends on the field value
}
and atomic_field = with_meta_t atomic_field'
and field' =
| AtomicField of atomic_field
| RecordField : record -> ident -> field'
| SwitchCaseField : switch_case -> ident -> field'
and field = with_meta_t field'
and record = list field
and case =
| Case : expr -> field -> case
| DefaultCase : field -> case
and switch_case = expr & list case
[@@ PpxDerivingYoJson ]
type attribute =
| Entrypoint
| Aligned
/// Typedefs are given 2 names by convention and can be tagged as an
/// "entrypoint" for the validator
///
/// E.g.,
/// typedef [entrypoint] struct _T { ... } T, *PTR_T;
[@@ PpxDerivingYoJson ]
noeq
type typedef_names = {
typedef_name: ident;
typedef_abbrev: ident;
typedef_ptr_abbrev: ident;
typedef_attributes: list attribute
}
[@@ PpxDerivingYoJson ]
let enum_case = ident & option (either int ident)
/// Specification of output types
///
/// Output types contain atomic fields with optional bitwidths for bitfield types,
/// but they may also contain anonymous structs and unions
[@@ PpxDerivingYoJson ]
noeq
type out_field =
| Out_field_named: ident -> typ -> bit_width:option int -> out_field
| Out_field_anon : list out_field -> is_union:bool -> out_field
[@@ PpxDerivingYoJson ]
noeq
type out_typ = {
out_typ_names : typedef_names;
out_typ_fields : list out_field;
out_typ_is_union : bool; //TODO: unclear if this field is needed
}
/// A 3d specification a list of declarations
/// - Define: macro definitions for constants
/// - TypeAbbrev: macro definition of types
/// - Enum: enumerated type using existing constants or newly defined constants
/// - Record: a struct with refinements
/// - CaseType: an untagged union
///
/// - OutputType: an output type definition
/// no validators are generated for these types,
/// they are used only in the parse trees construction in the actions
/// - ExternType: An abstract type declaration
/// - ExternFn: An abstract function declaration, may be used in the actions
[@@ PpxDerivingYoJson ]
noeq
type decl' =
| ModuleAbbrev: ident -> ident -> decl'
| Define: ident -> option typ -> constant -> decl'
| TypeAbbrev: typ -> ident -> decl'
| Enum: typ -> ident -> list enum_case -> decl'
| Record: names:typedef_names -> params:list param -> where:option expr -> fields:record -> decl'
| CaseType: typedef_names -> list param -> switch_case -> decl'
| OutputType : out_typ -> decl'
| ExternType : typedef_names -> decl'
| ExternFn : ident -> typ -> list param -> decl'
[@@ PpxDerivingYoJson ]
noeq
type decl = {
d_decl : with_meta_t decl';
d_exported : bool
}
let mk_decl (d:decl') r c (is_exported:bool) : decl =
{ d_decl = with_range_and_comments d r c;
d_exported = is_exported }
let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } }
[@@ PpxDerivingYoJson ]
noeq
type type_refinement = {
includes:list string;
type_map:list (ident * option ident)
}
[@@ PpxDerivingYoJson ]
let prog = list decl & option type_refinement
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
(** Entrypoint and export definitions *)
let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l)
let is_entrypoint_or_export d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
if has_entrypoint (names.typedef_attributes)
then true
else d.d_exported
| _ -> d.d_exported
let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false
(** Determine if there are output type expressions: which cases in
TranslateForInterpreter introduce the Output_type_expr constructor?
*)
/// Matches translate_action_assignment, translate_action_field_ptr_after
let out_expr_is_out_type_expr (lhs: out_expr) : Tot bool =
match lhs.out_expr_node.v with
| OE_star ({out_expr_node = {v=OE_id i}}) -> false
| _ -> true
/// Matches translate_atomic_action
let atomic_action_has_out_expr (a: atomic_action) : Tot bool =
match a with
| Action_field_ptr_after _ write_to
| Action_assignment write_to _
-> out_expr_is_out_type_expr write_to
| _ -> false
/// Matches translate_action
let rec action_has_out_expr (a: action) : Tot bool =
match a.v with
| Atomic_action a -> atomic_action_has_out_expr a
| Action_seq hd tl ->
if atomic_action_has_out_expr hd
then true
else action_has_out_expr tl
| Action_ite _ then_ (Some else_) ->
if action_has_out_expr then_
then true
else action_has_out_expr else_
| Action_ite _ then_ None ->
action_has_out_expr then_
| Action_let _ a k ->
if atomic_action_has_out_expr a
then true
else action_has_out_expr k
| Action_act a ->
action_has_out_expr a
let field_action_has_out_expr
(f: option (action & bool))
: Tot bool
= match f with
| None -> false
| Some (a, _) -> action_has_out_expr a
/// Matches translate_atomic_field
let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action
/// Matches field_as_grouped_fields
let rec field_has_out_expr (f: field) : Tot bool =
match f.v with
| AtomicField af ->
atomic_field_has_out_expr af
| RecordField fs _ ->
record_has_out_expr fs
| SwitchCaseField sw _ ->
switch_case_has_out_expr sw
and record_has_out_expr (fs: record) : Tot bool =
match fs with
| [] -> false
| f :: fs' ->
if field_has_out_expr f
then true
else record_has_out_expr fs'
and switch_case_has_out_expr (sw: switch_case) : Tot bool =
let (_, c) = sw in
cases_have_out_expr c
and cases_have_out_expr (cs: list case) : Tot bool =
match cs with
| [] -> false
| c :: cs ->
if case_has_out_expr c
then true
else cases_have_out_expr cs
and case_has_out_expr (c: case) : Tot bool =
match c with
| Case _ f
| DefaultCase f
->
field_has_out_expr f
/// Matches parse_field | false | true | Ast.fst | {
"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"
} | null | val decl_has_out_expr (d: decl) : Tot bool | [] | Ast.decl_has_out_expr | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | d: Ast.decl -> Prims.bool | {
"end_col": 14,
"end_line": 710,
"start_col": 2,
"start_line": 705
} |
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.All",
"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
}
] | false | let field_typ = t:typ { Type_app? t.v } | let field_typ = | false | null | false | t: typ{Type_app? t.v} | {
"checked_file": "Ast.fst.checked",
"dependencies": [
"prims.fst.checked",
"Options.fsti.checked",
"Hashtable.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.String.fsti.checked",
"FStar.ST.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.All.fst.checked"
],
"interface_file": false,
"source_file": "Ast.fst"
} | [
"total"
] | [
"Ast.typ",
"Prims.b2t",
"Ast.uu___is_Type_app",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.typ'"
] | [] | (*
Copyright 2019 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 Ast
(* The source abstract syntax for the 3d frontend to EverParse *)
open FStar.All
let reserved_prefix = "___"
//redefining either because we need to serialize to Json
[@@ PpxDerivingYoJson ]
type either a b =
| Inl of a
| Inr of b
/// pos: Source locations
type pos = {
filename: string;
line:int;
col:int
}
noeq
type comments_buffer_t = {
push: string & pos & pos -> ML unit;
flush: unit -> ML (list string);
flush_until: pos -> ML (list string);
}
#push-options "--warn_error -272" //top-level effect; ok
let comments_buffer : comments_buffer_t =
let buffer : ref (list (string & pos & pos)) = FStar.ST.alloc [] in
let buffer_comment (c, s, p) =
let c = String.substring c 2 (String.length c - 2) in
buffer := (c, s, p) :: !buffer
in
let flush_comments () =
let cs = !buffer in
buffer := [];
(List.rev cs) |> List.map (fun (c, _, _) -> c)
in
let flush_until pos : ML (list string) =
let cs = !buffer in
let preceding, following =
List.partition (fun (c, _, end_pos) ->
Options.debug_print_string
(Printf.sprintf "Requesting comments until line %d,\nAt line %d we have comment (*%s*)\n"
pos.line
end_pos.line
c);
end_pos.line <= pos.line) cs
in
buffer := following;
preceding |> List.map (fun (c, _, _) -> c)
in
{
push = buffer_comment;
flush = flush_comments;
flush_until = flush_until
}
#pop-options
let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col
/// range: A source extent
let range = pos * pos
/// comment: A list of line comments, i.e., a list of strings
let comments = list string
let string_of_range r =
let p, q = r in
if p.filename = q.filename
then Printf.sprintf "%s:(%d,%d--%d,%d)"
p.filename p.line p.col q.line q.col
else Printf.sprintf "%s -- %s"
(string_of_pos p)
(string_of_pos q)
let dummy_pos = {
filename="";
line=0;
col=0;
}
noeq
type with_meta_t 'a = {
v:'a;
range:range;
comments: comments
}
(* Override the json serializers for with_meta_t to
avoid polluting the generated JSON with ranges everywhere *)
let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson"
let with_meta_t_to_yojson (f:('a -> 'b)) (x:with_meta_t 'a)
: 'b
= f x.v
let with_range_and_comments (x:'a) r c : with_meta_t 'a = {
v = x;
range = r;
comments = c
}
let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r []
[@@ PpxDerivingYoJson ]
type ident' = {
modul_name : option string;
name : string
}
[@@ PpxDerivingYoJson ]
let ident = with_meta_t ident'
let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name
let ident_name i = i.v.name
exception Error of string
let error #a msg (r:range) : ML a =
raise (Error (Printf.sprintf "%s: (Error) %s\n" (string_of_pos (fst r)) msg))
let warning msg (r:range) : ML unit =
FStar.IO.print_string (Printf.sprintf "%s: (Warning) %s\n" (string_of_pos (fst r)) msg)
let check_reserved_identifier (i:ident) =
let open FStar.String in
let s = i.v.name in
if length s >= 3
&& sub s 0 3 = reserved_prefix
then error "Identifiers cannot begin with \"___\"" i.range
[@@ PpxDerivingYoJson ]
type integer_type =
| UInt8
| UInt16
| UInt32
| UInt64
let parse_int_suffix (i:string) : string * option integer_type =
let l = String.length i in
if l >= 2
then let suffix = String.sub i (l - 2) 2 in
let prefix = String.sub i 0 (l - 2) in
match suffix with
| "uy" -> prefix, Some UInt8
| "us" -> prefix, Some UInt16
| "ul" -> prefix, Some UInt32
| "uL" -> prefix, Some UInt64
| _ -> i, None
else i, None
let smallest_integer_type_of r (i:int) : ML integer_type =
if FStar.UInt.fits i 8 then UInt8
else if FStar.UInt.fits i 16 then UInt16
else if FStar.UInt.fits i 32 then UInt32
else if FStar.UInt.fits i 64 then UInt64
else error (Printf.sprintf
"Integer %d is too large for all supported fixed-width types"
i)
r
let integer_type_lub (t1 t2: integer_type) : Tot integer_type =
match t1, t2 with
| UInt64, _
| _, UInt64 -> UInt64
| _, UInt32
| UInt32, _ -> UInt32
| _, UInt16
| UInt16, _ -> UInt16
| UInt8, UInt8 -> UInt8
let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2
let maybe_as_integer_typ (i:ident) : Tot (option integer_type) =
if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some UInt8
| "UINT16" -> Some UInt16
| "UINT32" -> Some UInt32
| "UINT64" -> Some UInt64
| "UINT8BE" -> Some UInt8
| "UINT16BE" -> Some UInt16
| "UINT32BE" -> Some UInt32
| "UINT64BE" -> Some UInt64
| _ -> None
let as_integer_typ (i:ident) : ML integer_type =
match maybe_as_integer_typ i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Bit order for bitfields
[@@ PpxDerivingYoJson ]
type bitfield_bit_order =
| LSBFirst (* Least-significant bit first (MSVC default) *)
| MSBFirst (* Most-significant bit first (necessary for many IETF protocols) *)
let maybe_bit_order_of (i:ident) : Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y ->
Some? y == Some? (maybe_as_integer_typ i)
))
= if i.v.modul_name <> None
then None
else
match i.v.name with
| "UINT8" -> Some LSBFirst
| "UINT16" -> Some LSBFirst
| "UINT32" -> Some LSBFirst
| "UINT64" -> Some LSBFirst
| "UINT8BE" -> Some MSBFirst
| "UINT16BE" -> Some MSBFirst
| "UINT32BE" -> Some MSBFirst
| "UINT64BE" -> Some MSBFirst
| _ -> None
let bit_order_of (i:ident) : ML bitfield_bit_order =
match maybe_bit_order_of i with
| None -> error ("Unknown integer type: " ^ ident_to_string i) i.range
| Some t -> t
/// Integer, hex and boolean constants
[@@ PpxDerivingYoJson ]
type constant =
| Unit
| Int : integer_type -> int -> constant
| XInt: integer_type -> string -> constant //hexadecimal constants
| Bool of bool
/// Operators supported in refinement expressions
[@@ PpxDerivingYoJson ]
type op =
| Eq
| Neq
| And
| Or
| Not
| Plus of option integer_type
| Minus of option integer_type
| Mul of option integer_type
| Division of option integer_type
| Remainder of option integer_type
| BitwiseAnd of option integer_type
| BitwiseXor of option integer_type
| BitwiseOr of option integer_type
| BitwiseNot of option integer_type
| ShiftRight of option integer_type
| ShiftLeft of option integer_type
| LT of option integer_type
| GT of option integer_type
| LE of option integer_type
| GE of option integer_type
| IfThenElse
| BitFieldOf: sz: int -> order: bitfield_bit_order -> op //BitFieldOf_n(i, from, to); the integer is the size of i in bits
| SizeOf
| Cast : from:option integer_type -> to:integer_type -> op
| Ext of string
//OffsetOf ?
/// Expressions used in refinements
/// Expressions have no binding structure
/// Names are represented using concrete identifiers, i.e., strings
/// We enforce that all names are unique in a scope, i.e., no shadowing allowed
[@@ PpxDerivingYoJson ]
noeq
type expr' =
| Constant of constant
| Identifier of ident
| Static of expr //the guard of a #if; must be made from compile-time constants only
| This
| App : op -> list expr -> expr'
and expr = with_meta_t expr'
/// A non-pointer type in the AST (see typ below) may be
/// - A spec type, i.e. a type that has an interpretation in 3d, that 3d understands
/// - An output type, may be used as the type of the parse tree constructed as part of actions
/// This includes structs and unions, and actions support assignment to fields of output types
/// - An extern type, an abstract, uninterpreted type
[@@ PpxDerivingYoJson ]
type t_kind =
| KindSpec
| KindOutput
| KindExtern
/// Syntax for output expressions
///
/// Output expressions may appear as type parameters or as lhs of assignment actions
[@@ PpxDerivingYoJson ]
noeq
type out_expr' =
| OE_id : ident -> out_expr'
| OE_star : out_expr -> out_expr'
| OE_addrof : out_expr -> out_expr'
| OE_deref : out_expr -> ident -> out_expr' //deref a field
| OE_dot : out_expr -> ident -> out_expr' //read a field
/// Output expressions maintain metadata
/// (base type, type of the expr and its bitwidth, in case the expression is of a bitfield type)
///
/// where base type is the type of the base identifier,
/// and type of the output expression
///
/// The metadata is initially None after parsing,
/// and is populated after typechecking (in Binding.fst)
///
/// It is used during emitting F* and C code
/// For each output expression, we emit an action (external function call)
/// whose signature requires all this
///
/// TODO: could we also store the source string for pretty printing?
and out_expr_meta_t = {
out_expr_base_t : typ;
out_expr_t : typ;
out_expr_bit_width : option int;
}
and out_expr = { out_expr_node: with_meta_t out_expr';
out_expr_meta: option out_expr_meta_t }
/// A type parameter is either an expression or an output expression
and typ_param = either expr out_expr
/// Types: all types are named and fully instantiated to expressions only
/// i.e., no type-parameterized types
///
/// The t_kind field maintains the kind
///
/// It is set during the desugaring phase, the parser always sets it to KindSpec
/// We could move it to the parser itself
///
/// Keeping this makes it easy to check whether a type is an output type or an extern type
/// Alternatively we would have to carry some environment along
and typ' =
| Type_app : ident -> t_kind -> list typ_param -> typ'
| Pointer : typ -> typ'
and typ = with_meta_t typ' | false | true | Ast.fst | {
"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"
} | null | val field_typ : Type0 | [] | Ast.field_typ | {
"file_name": "src/3d/Ast.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Type0 | {
"end_col": 39,
"end_line": 369,
"start_col": 16,
"start_line": 369
} |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.