file_name
stringlengths 5
52
| name
stringlengths 4
95
| original_source_type
stringlengths 0
23k
| source_type
stringlengths 9
23k
| source_definition
stringlengths 9
57.9k
| source
dict | source_range
dict | file_context
stringlengths 0
721k
| dependencies
dict | opens_and_abbrevs
listlengths 2
94
| vconfig
dict | interleaved
bool 1
class | verbose_type
stringlengths 1
7.42k
| effect
stringclasses 118
values | effect_flags
sequencelengths 0
2
| mutual_with
sequencelengths 0
11
| ideal_premises
sequencelengths 0
236
| proof_features
sequencelengths 0
1
| is_simple_lemma
bool 2
classes | is_div
bool 2
classes | is_proof
bool 2
classes | is_simply_typed
bool 2
classes | is_type
bool 2
classes | partial_definition
stringlengths 5
3.99k
| completed_definiton
stringlengths 1
1.63M
| isa_cross_project_example
bool 1
class |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
LowStar.Vector.fst | LowStar.Vector.modifies_as_seq | 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)] | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 49,
"end_line": 232,
"start_col": 0,
"start_line": 231
} | (*
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); | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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)
] | FStar.Pervasives.Lemma | [
"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"
] | [] | true | false | true | false | false | let modifies_as_seq #a vec dloc h0 h1 =
| B.modifies_buffer_elim (Vec?.vs vec) dloc h0 h1 | false |
LowStar.Vector.fst | LowStar.Vector.loc_vector_within | 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))) | 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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 52,
"end_line": 115,
"start_col": 0,
"start_line": 112
} | (*
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} -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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 | Prims.GTot | [
"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"
] | [
"recursion"
] | false | false | false | false | 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) | false |
LowStar.Vector.fst | LowStar.Vector.alloc_empty | val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul}) | val alloc_empty:
a:Type -> Tot (vec:vector a{size_of vec = 0ul}) | let alloc_empty a =
Vec 0ul 0ul B.null | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 20,
"end_line": 265,
"start_col": 22,
"start_line": 264
} | (*
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: | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Type0 -> vec: LowStar.Vector.vector a {LowStar.Vector.size_of vec = 0ul} | Prims.Tot | [
"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"
] | [] | false | false | false | false | false | let alloc_empty a =
| Vec 0ul 0ul B.null | false |
Vale.Transformers.Locations.fst | Vale.Transformers.Locations.aux_print_reg_from_location | val aux_print_reg_from_location (a: location{ALocReg? a}) : string | val aux_print_reg_from_location (a: location{ALocReg? a}) : string | 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 | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 25,
"end_line": 28,
"start_col": 0,
"start_line": 24
} | 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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | a: Vale.Transformers.Locations.location{ALocReg? a} -> Prims.string | Prims.Tot | [
"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"
] | [] | false | false | false | false | 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 | false |
Vale.Transformers.Locations.fst | Vale.Transformers.Locations.location_val_eqt | val location_val_eqt : location -> eqtype | val location_val_eqt : location -> eqtype | 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 | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 24,
"end_line": 65,
"start_col": 0,
"start_line": 59
} | 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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | a: Vale.Transformers.Locations.location -> Prims.eqtype | Prims.Tot | [
"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"
] | [] | false | false | false | true | 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 | false |
LowStar.Vector.fst | LowStar.Vector.index | 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)) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 25,
"end_line": 367,
"start_col": 0,
"start_line": 366
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | vec: LowStar.Vector.vector a -> i: LowStar.Vector.uint32_t -> FStar.HyperStack.ST.ST a | FStar.HyperStack.ST.ST | [] | [] | [
"LowStar.Vector.vector",
"LowStar.Vector.uint32_t",
"LowStar.Monotonic.Buffer.index",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs"
] | [] | false | true | false | false | false | let index #a vec i =
| B.index (Vec?.vs vec) i | false |
Vale.Transformers.Locations.fst | Vale.Transformers.Locations.lemma_locations_complete | 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)) | 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 =
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) | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 37,
"end_line": 117,
"start_col": 0,
"start_line": 99
} | 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 = () | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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) | FStar.Pervasives.Lemma | [
"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"
] | [] | false | false | true | false | 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))
<:
(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) | false |
Hacl.Test.HMAC_DRBG.fst | Hacl.Test.HMAC_DRBG.vec8 | val vec8 : Type0 | let vec8 = L.lbuffer UInt8.t | {
"file_name": "code/tests/Hacl.Test.HMAC_DRBG.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 28,
"end_line": 31,
"start_col": 0,
"start_line": 31
} | module Hacl.Test.HMAC_DRBG
open FStar.HyperStack.ST
open Test.Lowstarize
open Lib.IntTypes
open Hacl.HMAC_DRBG
open Spec.HMAC_DRBG.Test.Vectors
module D = Spec.Hash.Definitions
module L = Test.Lowstarize
module B = LowStar.Buffer
#set-options "--fuel 0 --ifuel 0 --z3rlimit 100"
(* FStar.Reflection only supports up to 8-tuples *)
noextract
let vectors_tmp = List.Tot.map
(fun x -> x.a, h x.entropy_input, h x.nonce, h x.personalization_string,
h x.entropy_input_reseed, h x.additional_input_reseed,
(h x.additional_input_1, h x.additional_input_2),
h x.returned_bits)
test_vectors
%splice[vectors_low] (lowstarize_toplevel "vectors_tmp" "vectors_low")
// Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t
assume val declassify_uint8: squash (uint8 == UInt8.t) | {
"checked_file": "/",
"dependencies": [
"Test.Lowstarize.fst.checked",
"Spec.HMAC_DRBG.Test.Vectors.fst.checked",
"Spec.HMAC_DRBG.fsti.checked",
"Spec.Hash.Definitions.fst.checked",
"prims.fst.checked",
"LowStar.Printf.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Hacl.HMAC_DRBG.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Int32.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"C.String.fsti.checked",
"C.Loops.fst.checked",
"C.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Test.HMAC_DRBG.fst"
} | [
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "Test.Lowstarize",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Spec.Hash.Definitions",
"short_module": "D"
},
{
"abbrev": false,
"full_module": "Spec.HMAC_DRBG.Test.Vectors",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HMAC_DRBG",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Test.Lowstarize",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Test",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Test",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Test.Lowstarize.lbuffer",
"FStar.UInt8.t"
] | [] | false | false | false | true | true | let vec8 =
| L.lbuffer UInt8.t | false |
|
LowStar.Vector.fst | LowStar.Vector.free | 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)) | 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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 22,
"end_line": 350,
"start_col": 0,
"start_line": 349
} | (*
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)) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | vec: LowStar.Vector.vector a -> FStar.HyperStack.ST.ST Prims.unit | FStar.HyperStack.ST.ST | [] | [] | [
"LowStar.Vector.vector",
"LowStar.Monotonic.Buffer.free",
"LowStar.Buffer.trivial_preorder",
"LowStar.Vector.__proj__Vec__item__vs",
"Prims.unit"
] | [] | false | true | false | false | false | let free #a vec =
| B.free (Vec?.vs vec) | false |
LowStar.Vector.fst | LowStar.Vector.clear | val clear:
#a:Type -> vec:vector a ->
Tot (cvec:vector a{size_of cvec = 0ul}) | 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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 38,
"end_line": 393,
"start_col": 0,
"start_line": 392
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | vec: LowStar.Vector.vector a -> cvec: LowStar.Vector.vector a {LowStar.Vector.size_of cvec = 0ul} | Prims.Tot | [
"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"
] | [] | false | false | false | false | false | let clear #a vec =
| Vec 0ul (Vec?.cap vec) (Vec?.vs vec) | false |
LowStar.Vector.fst | LowStar.Vector.loc_vector_within_included | 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))) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 49,
"end_line": 166,
"start_col": 0,
"start_line": 164
} | (*
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))) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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)) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | false | let rec loc_vector_within_included #a vec i j =
| if i = j then () else loc_vector_within_included vec (i + 1ul) j | false |
LowStar.Vector.fst | LowStar.Vector.loc_vector_within_includes_ | 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))) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 137,
"start_col": 0,
"start_line": 126
} | (*
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))) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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)) | FStar.Pervasives.Lemma | [
"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_"
] | [
"recursion"
] | false | false | true | false | false | let rec loc_vector_within_includes_ #a vec i j1 j2 =
| 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)) | false |
LowStar.Vector.fst | LowStar.Vector.alloc_reserve | 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)) | 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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 35,
"end_line": 324,
"start_col": 0,
"start_line": 323
} | (*
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 /\ | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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) | FStar.HyperStack.ST.ST | [] | [] | [
"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"
] | [] | false | true | false | false | false | let alloc_reserve #a len ia rid =
| Vec 0ul len (B.malloc rid ia len) | false |
LowStar.Vector.fst | LowStar.Vector.front | 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)) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 27,
"end_line": 376,
"start_col": 0,
"start_line": 375
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | vec: LowStar.Vector.vector a {LowStar.Vector.size_of vec > 0ul} -> FStar.HyperStack.ST.ST a | FStar.HyperStack.ST.ST | [] | [] | [
"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"
] | [] | false | true | false | false | false | let front #a vec =
| B.index (Vec?.vs vec) 0ul | false |
Vale.Transformers.Locations.fst | Vale.Transformers.Locations.eval_location | val eval_location :
a:location ->
machine_state ->
location_val_t a | val eval_location :
a:location ->
machine_state ->
location_val_t a | 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) | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 60,
"end_line": 74,
"start_col": 0,
"start_line": 68
} | 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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | a: Vale.Transformers.Locations.location -> s: Vale.X64.Machine_Semantics_s.machine_state
-> Vale.Transformers.Locations.location_val_t a | Prims.Tot | [
"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"
] | [] | false | false | false | false | 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) | false |
LowStar.Vector.fst | LowStar.Vector.get | val get:
#a:Type -> h:HS.mem -> vec:vector a ->
i:uint32_t{i < size_of vec} -> GTot a | 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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 358,
"start_col": 0,
"start_line": 357
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h: FStar.Monotonic.HyperStack.mem ->
vec: LowStar.Vector.vector a ->
i: LowStar.Vector.uint32_t{i < LowStar.Vector.size_of vec}
-> Prims.GTot a | Prims.GTot | [
"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"
] | [] | false | false | false | false | false | let get #a h vec i =
| S.index (as_seq h vec) (U32.v i) | false |
LowStar.Vector.fst | LowStar.Vector.loc_vector_within_disjoint_ | 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))) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 55,
"end_line": 179,
"start_col": 0,
"start_line": 177
} | (*
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))) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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)) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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 | false |
LowStar.Vector.fst | LowStar.Vector.forall_buffer | 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 | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 35,
"end_line": 562,
"start_col": 0,
"start_line": 561
} | (*
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} -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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 | Prims.GTot | [
"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"
] | [] | false | false | false | false | true | let forall_buffer #a h buf i j p =
| forall_seq (B.as_seq h buf) i j p | false |
LowStar.Vector.fst | LowStar.Vector.alloc_by_buffer | 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))) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 17,
"end_line": 340,
"start_col": 0,
"start_line": 339
} | (*
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 /\ | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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) | FStar.HyperStack.ST.ST | [] | [] | [
"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"
] | [] | false | true | false | false | false | let alloc_by_buffer #a len buf =
| Vec len len buf | false |
LowStar.Vector.fst | LowStar.Vector.shrink | val shrink:
#a:Type -> vec:vector a ->
new_size:uint32_t{new_size <= size_of vec} ->
Tot (vector a) | 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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 43,
"end_line": 510,
"start_col": 0,
"start_line": 509
} | (*
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} -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
vec: LowStar.Vector.vector a ->
new_size: LowStar.Vector.uint32_t{new_size <= LowStar.Vector.size_of vec}
-> LowStar.Vector.vector a | Prims.Tot | [
"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"
] | [] | false | false | false | false | false | let shrink #a vec new_size =
| Vec new_size (Vec?.cap vec) (Vec?.vs vec) | false |
LowStar.Vector.fst | LowStar.Vector.alloc | 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)) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 25,
"end_line": 306,
"start_col": 0,
"start_line": 305
} | (*
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) /\ | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | len: LowStar.Vector.uint32_t{len > 0ul} -> v: a -> FStar.HyperStack.ST.ST (LowStar.Vector.vector a) | FStar.HyperStack.ST.ST | [] | [] | [
"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"
] | [] | false | true | false | false | false | let alloc #a len v =
| alloc_rid len v HS.root | false |
LowStar.Vector.fst | LowStar.Vector.loc_vector_within_disjoint | 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))) | 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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 58,
"end_line": 192,
"start_col": 0,
"start_line": 189
} | (*
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))) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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)) | FStar.Pervasives.Lemma | [
"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_"
] | [
"recursion"
] | false | false | true | false | 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) | false |
LowStar.Vector.fst | LowStar.Vector.fold_left_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)) | 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)) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 55,
"end_line": 521,
"start_col": 0,
"start_line": 519
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | seq: FStar.Seq.Base.seq a -> f: (_: b -> _: a -> Prims.GTot b) -> ib: b -> Prims.GTot b | Prims.GTot | [
"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"
] | [
"recursion"
] | false | false | false | false | 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)) | false |
LowStar.Vector.fst | LowStar.Vector.forall_all | val forall_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> Tot Type0) -> GTot Type0 | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 35,
"end_line": 575,
"start_col": 0,
"start_line": 574
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | h: FStar.Monotonic.HyperStack.mem -> vec: LowStar.Vector.vector a -> p: (_: a -> Type0)
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.vector",
"LowStar.Vector.forall_",
"FStar.UInt32.__uint_to_t",
"LowStar.Vector.size_of"
] | [] | false | false | false | false | true | let forall_all #a h vec p =
| forall_ h vec 0ul (size_of vec) p | false |
LowStar.Vector.fst | LowStar.Vector.new_capacity | val new_capacity: cap:uint32_t -> Tot uint32_t | val new_capacity: cap:uint32_t -> Tot uint32_t | let new_capacity cap =
if cap >= max_uint32 / resize_ratio then max_uint32
else if cap = 0ul then 1ul
else cap * resize_ratio | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 25,
"end_line": 444,
"start_col": 8,
"start_line": 441
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | cap: LowStar.Vector.uint32_t -> LowStar.Vector.uint32_t | Prims.Tot | [
"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"
] | [] | false | false | false | true | false | let new_capacity cap =
| if cap >= max_uint32 / resize_ratio
then max_uint32
else if cap = 0ul then 1ul else cap * resize_ratio | false |
LowStar.Vector.fst | LowStar.Vector.forall2_buffer | 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 | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 590,
"start_col": 0,
"start_line": 589
} | (*
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} -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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 | Prims.GTot | [
"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"
] | [] | false | false | false | false | true | let forall2_buffer #a h buf i j p =
| forall2_seq (B.as_seq h buf) i j p | false |
LowStar.Vector.fst | LowStar.Vector.fold_left | 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)) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 77,
"end_line": 547,
"start_col": 0,
"start_line": 546
} | (*
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 /\ | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | vec: LowStar.Vector.vector a -> f: (_: b -> _: a -> b) -> ib: b -> FStar.HyperStack.ST.ST b | FStar.HyperStack.ST.ST | [] | [] | [
"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"
] | [] | false | true | false | false | 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 | false |
LowStar.Vector.fst | LowStar.Vector.back | 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)) | 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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 43,
"end_line": 385,
"start_col": 0,
"start_line": 384
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | vec: LowStar.Vector.vector a {LowStar.Vector.size_of vec > 0ul} -> FStar.HyperStack.ST.ST a | FStar.HyperStack.ST.ST | [] | [] | [
"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"
] | [] | false | true | false | false | false | let back #a vec =
| B.index (Vec?.vs vec) (size_of vec - 1ul) | false |
Hacl.Test.HMAC_DRBG.fst | Hacl.Test.HMAC_DRBG.vector | val vector : Type0 | let vector = D.hash_alg & vec8 & vec8 & vec8 & vec8 & vec8 & (vec8 & vec8) & vec8 | {
"file_name": "code/tests/Hacl.Test.HMAC_DRBG.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 81,
"end_line": 33,
"start_col": 0,
"start_line": 33
} | module Hacl.Test.HMAC_DRBG
open FStar.HyperStack.ST
open Test.Lowstarize
open Lib.IntTypes
open Hacl.HMAC_DRBG
open Spec.HMAC_DRBG.Test.Vectors
module D = Spec.Hash.Definitions
module L = Test.Lowstarize
module B = LowStar.Buffer
#set-options "--fuel 0 --ifuel 0 --z3rlimit 100"
(* FStar.Reflection only supports up to 8-tuples *)
noextract
let vectors_tmp = List.Tot.map
(fun x -> x.a, h x.entropy_input, h x.nonce, h x.personalization_string,
h x.entropy_input_reseed, h x.additional_input_reseed,
(h x.additional_input_1, h x.additional_input_2),
h x.returned_bits)
test_vectors
%splice[vectors_low] (lowstarize_toplevel "vectors_tmp" "vectors_low")
// Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t
assume val declassify_uint8: squash (uint8 == UInt8.t)
let vec8 = L.lbuffer UInt8.t | {
"checked_file": "/",
"dependencies": [
"Test.Lowstarize.fst.checked",
"Spec.HMAC_DRBG.Test.Vectors.fst.checked",
"Spec.HMAC_DRBG.fsti.checked",
"Spec.Hash.Definitions.fst.checked",
"prims.fst.checked",
"LowStar.Printf.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Hacl.HMAC_DRBG.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Int32.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"C.String.fsti.checked",
"C.Loops.fst.checked",
"C.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Test.HMAC_DRBG.fst"
} | [
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "Test.Lowstarize",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Spec.Hash.Definitions",
"short_module": "D"
},
{
"abbrev": false,
"full_module": "Spec.HMAC_DRBG.Test.Vectors",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HMAC_DRBG",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Test.Lowstarize",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Test",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Test",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple8",
"Spec.Hash.Definitions.hash_alg",
"Hacl.Test.HMAC_DRBG.vec8",
"FStar.Pervasives.Native.tuple2"
] | [] | false | false | false | true | true | let vector =
| D.hash_alg & vec8 & vec8 & vec8 & vec8 & vec8 & (vec8 & vec8) & vec8 | false |
|
LowStar.Vector.fst | LowStar.Vector.forall_seq | 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 | 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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 23,
"end_line": 555,
"start_col": 0,
"start_line": 553
} | (*
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} -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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 | Prims.GTot | [
"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"
] | [] | false | false | false | false | true | let forall_seq #a seq i j p =
| forall (idx: nat{i <= idx && idx < j}). p (S.index seq idx) | false |
LowStar.Vector.fst | LowStar.Vector.forall_ | 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 | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 49,
"end_line": 569,
"start_col": 0,
"start_line": 568
} | (*
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} -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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 | Prims.GTot | [
"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"
] | [] | false | false | false | false | true | let forall_ #a h vec i j p =
| forall_seq (as_seq h vec) (U32.v i) (U32.v j) p | false |
Vale.Transformers.Locations.fst | Vale.Transformers.Locations.update_location | val update_location :
a:location ->
v:location_val_t a ->
machine_state ->
s:machine_state{eval_location a s == v} | 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 =
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) } | {
"file_name": "vale/code/lib/transformers/Vale.Transformers.Locations.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 121,
"end_line": 93,
"start_col": 0,
"start_line": 77
} | 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) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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} | Prims.Tot | [
"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"
] | [] | false | false | false | false | 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)
} | false |
LowStar.Vector.fst | LowStar.Vector.loc_vector_within_includes | 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))) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 155,
"start_col": 0,
"start_line": 147
} | (*
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))) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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)) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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
(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)) | false |
LowStar.Vector.fst | LowStar.Vector.forall2 | 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 | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 50,
"end_line": 597,
"start_col": 0,
"start_line": 596
} | (*
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} -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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 | Prims.GTot | [
"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"
] | [] | false | false | false | false | true | let forall2 #a h vec i j p =
| forall2_seq (as_seq h vec) (U32.v i) (U32.v j) p | false |
LowStar.Vector.fst | LowStar.Vector.loc_vector_within_union_rev | 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))) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 209,
"start_col": 0,
"start_line": 202
} | (*
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))) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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)) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | false | let rec loc_vector_within_union_rev #a vec i j =
| 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)) | false |
LowStar.Vector.fst | LowStar.Vector.forall2_seq | 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 | 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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 583,
"start_col": 0,
"start_line": 581
} | (*
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} -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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 | Prims.GTot | [
"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"
] | [] | false | false | false | false | true | 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) | false |
CalcTest.fst | CalcTest.calc0 | val calc0 (a: pos) : Lemma (a + a > a) | val calc0 (a: pos) : Lemma (a + a > a) | let calc0 (a : pos) : Lemma (a + a > a) =
calc (>) {
a + a;
== {}
2 * a;
> { lem1 a }
a;
} | {
"file_name": "examples/calc/CalcTest.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 15,
"start_col": 0,
"start_line": 8
} | module CalcTest
open FStar.Mul
open FStar.Calc
let lem1 (a : pos) : Lemma (2 * a > a) = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "CalcTest.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Prims.pos -> FStar.Pervasives.Lemma (ensures a + a > a) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.Calc.calc_finish",
"Prims.int",
"Prims.b2t",
"Prims.op_GreaterThan",
"Prims.op_Addition",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.eq2",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Mul.op_Star",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"CalcTest.lem1",
"Prims.l_True",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let calc0 (a: pos) : Lemma (a + a > a) =
| calc ( > ) {
a + a;
( == ) { () }
2 * a;
( > ) { lem1 a }
a;
} | false |
CalcTest.fst | CalcTest.test_erase | val test_erase : _: Prims.unit -> Prims.int | let test_erase () =
let x = 1 in
calc (>=) {
10;
>= {}
9;
>= {}
8;
== {}
4+4;
>= {}
0;
};
let y = 41 in
x + y | {
"file_name": "examples/calc/CalcTest.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 7,
"end_line": 75,
"start_col": 0,
"start_line": 61
} | module CalcTest
open FStar.Mul
open FStar.Calc
let lem1 (a : pos) : Lemma (2 * a > a) = ()
let calc0 (a : pos) : Lemma (a + a > a) =
calc (>) {
a + a;
== {}
2 * a;
> { lem1 a }
a;
}
(* The lemma above, desugared. F* eta-expands and ascribes <: Type in
* order to treat boolean operators such as (>) as `relation`s. *)
let calc0_desugared (a : pos) : Lemma (a + a > a) =
calc_finish (fun x y -> (>) x y <: Type) (fun () ->
calc_step (fun x y -> (>) x y <: Type) a (fun () ->
calc_step (fun x y -> (==) x y <: Type) (2 * a) (fun () ->
calc_init (a + a)
) (fun () -> ())
) (fun () -> lem1 a)
)
let calc1 (a b c d e : int)
(h1 : unit -> Lemma (a * (c + 1) = 24))
(h2 : unit -> Lemma (b + d == 25))
: Lemma (a + b + a * c + d - (e - e) > 30) =
calc (>) {
a + b + a * c + d - (e - e);
== { (* reorder, e-e = 0 *) }
a + a * c + b + d;
== { (* distributivity *) }
a * (c + 1) + b + d;
== { h1 () }
24 + b + d;
== { h2 () }
24 + 25;
== { }
49;
> { }
30;
}
let test_ge () =
calc (>=) {
10;
>= {}
9;
>= {}
8;
== {}
4+4;
>= {}
0;
} | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "CalcTest.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"Prims.unit",
"Prims.op_Addition",
"Prims.int",
"FStar.Calc.calc_finish",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.eq2",
"Prims.Nil",
"FStar.Calc.calc_step",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash"
] | [] | false | false | false | true | false | let test_erase () =
| let x = 1 in
calc ( >= ) {
10;
( >= ) { () }
9;
( >= ) { () }
8;
( == ) { () }
4 + 4;
( >= ) { () }
0;
};
let y = 41 in
x + y | false |
|
LowStar.Vector.fst | LowStar.Vector.fold_left_buffer | 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)) | 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))) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 52,
"end_line": 536,
"start_col": 0,
"start_line": 533
} | (*
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)) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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 | FStar.HyperStack.ST.ST | [
""
] | [] | [
"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"
] | [
"recursion"
] | false | true | false | false | 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))) | false |
CalcTest.fst | CalcTest.calc0_desugared | val calc0_desugared (a: pos) : Lemma (a + a > a) | val calc0_desugared (a: pos) : Lemma (a + a > a) | let calc0_desugared (a : pos) : Lemma (a + a > a) =
calc_finish (fun x y -> (>) x y <: Type) (fun () ->
calc_step (fun x y -> (>) x y <: Type) a (fun () ->
calc_step (fun x y -> (==) x y <: Type) (2 * a) (fun () ->
calc_init (a + a)
) (fun () -> ())
) (fun () -> lem1 a)
) | {
"file_name": "examples/calc/CalcTest.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 26,
"start_col": 0,
"start_line": 19
} | module CalcTest
open FStar.Mul
open FStar.Calc
let lem1 (a : pos) : Lemma (2 * a > a) = ()
let calc0 (a : pos) : Lemma (a + a > a) =
calc (>) {
a + a;
== {}
2 * a;
> { lem1 a }
a;
}
(* The lemma above, desugared. F* eta-expands and ascribes <: Type in | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "CalcTest.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Prims.pos -> FStar.Pervasives.Lemma (ensures a + a > a) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.Calc.calc_finish",
"Prims.int",
"Prims.b2t",
"Prims.op_GreaterThan",
"Prims.op_Addition",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.eq2",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Mul.op_Star",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"CalcTest.lem1",
"Prims.l_True",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let calc0_desugared (a: pos) : Lemma (a + a > a) =
| calc_finish (fun x y -> ( > ) x y <: Type)
(fun () ->
calc_step (fun x y -> ( > ) x y <: Type)
a
(fun () ->
calc_step (fun x y -> ( == ) x y <: Type)
(2 * a)
(fun () -> calc_init (a + a))
(fun () -> ()))
(fun () -> lem1 a)) | false |
CalcTest.fst | CalcTest.test_ge | val test_ge : _: Prims.unit -> Prims.unit | let test_ge () =
calc (>=) {
10;
>= {}
9;
>= {}
8;
== {}
4+4;
>= {}
0;
} | {
"file_name": "examples/calc/CalcTest.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 59,
"start_col": 0,
"start_line": 48
} | module CalcTest
open FStar.Mul
open FStar.Calc
let lem1 (a : pos) : Lemma (2 * a > a) = ()
let calc0 (a : pos) : Lemma (a + a > a) =
calc (>) {
a + a;
== {}
2 * a;
> { lem1 a }
a;
}
(* The lemma above, desugared. F* eta-expands and ascribes <: Type in
* order to treat boolean operators such as (>) as `relation`s. *)
let calc0_desugared (a : pos) : Lemma (a + a > a) =
calc_finish (fun x y -> (>) x y <: Type) (fun () ->
calc_step (fun x y -> (>) x y <: Type) a (fun () ->
calc_step (fun x y -> (==) x y <: Type) (2 * a) (fun () ->
calc_init (a + a)
) (fun () -> ())
) (fun () -> lem1 a)
)
let calc1 (a b c d e : int)
(h1 : unit -> Lemma (a * (c + 1) = 24))
(h2 : unit -> Lemma (b + d == 25))
: Lemma (a + b + a * c + d - (e - e) > 30) =
calc (>) {
a + b + a * c + d - (e - e);
== { (* reorder, e-e = 0 *) }
a + a * c + b + d;
== { (* distributivity *) }
a * (c + 1) + b + d;
== { h1 () }
24 + b + d;
== { h2 () }
24 + 25;
== { }
49;
> { }
30;
} | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "CalcTest.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> Prims.unit | Prims.Tot | [
"total"
] | [] | [
"Prims.unit",
"FStar.Calc.calc_finish",
"Prims.int",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.eq2",
"Prims.Nil",
"FStar.Calc.calc_step",
"Prims.op_Addition",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash"
] | [] | false | false | false | true | false | let test_ge () =
| calc ( >= ) {
10;
( >= ) { () }
9;
( >= ) { () }
8;
( == ) { () }
4 + 4;
( >= ) { () }
0;
} | false |
|
CalcTest.fst | CalcTest.test_gt | val test_gt : _: Prims.unit -> Prims.unit | let test_gt () =
calc (>) {
10;
>= {}
9;
> { () }
8;
== {}
4+4;
>= {}
0;
} | {
"file_name": "examples/calc/CalcTest.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 88,
"start_col": 0,
"start_line": 77
} | module CalcTest
open FStar.Mul
open FStar.Calc
let lem1 (a : pos) : Lemma (2 * a > a) = ()
let calc0 (a : pos) : Lemma (a + a > a) =
calc (>) {
a + a;
== {}
2 * a;
> { lem1 a }
a;
}
(* The lemma above, desugared. F* eta-expands and ascribes <: Type in
* order to treat boolean operators such as (>) as `relation`s. *)
let calc0_desugared (a : pos) : Lemma (a + a > a) =
calc_finish (fun x y -> (>) x y <: Type) (fun () ->
calc_step (fun x y -> (>) x y <: Type) a (fun () ->
calc_step (fun x y -> (==) x y <: Type) (2 * a) (fun () ->
calc_init (a + a)
) (fun () -> ())
) (fun () -> lem1 a)
)
let calc1 (a b c d e : int)
(h1 : unit -> Lemma (a * (c + 1) = 24))
(h2 : unit -> Lemma (b + d == 25))
: Lemma (a + b + a * c + d - (e - e) > 30) =
calc (>) {
a + b + a * c + d - (e - e);
== { (* reorder, e-e = 0 *) }
a + a * c + b + d;
== { (* distributivity *) }
a * (c + 1) + b + d;
== { h1 () }
24 + b + d;
== { h2 () }
24 + 25;
== { }
49;
> { }
30;
}
let test_ge () =
calc (>=) {
10;
>= {}
9;
>= {}
8;
== {}
4+4;
>= {}
0;
}
let test_erase () =
let x = 1 in
calc (>=) {
10;
>= {}
9;
>= {}
8;
== {}
4+4;
>= {}
0;
};
let y = 41 in
x + y | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "CalcTest.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> Prims.unit | Prims.Tot | [
"total"
] | [] | [
"Prims.unit",
"FStar.Calc.calc_finish",
"Prims.int",
"Prims.b2t",
"Prims.op_GreaterThan",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.op_GreaterThanOrEqual",
"Prims.eq2",
"Prims.Nil",
"FStar.Calc.calc_step",
"Prims.op_Addition",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash"
] | [] | false | false | false | true | false | let test_gt () =
| calc ( > ) {
10;
( >= ) { () }
9;
( > ) { () }
8;
( == ) { () }
4 + 4;
( >= ) { () }
0;
} | false |
|
CalcTest.fst | CalcTest.calc1 | val calc1
(a b c d e: int)
(h1: (unit -> Lemma (a * (c + 1) = 24)))
(h2: (unit -> Lemma (b + d == 25)))
: Lemma (a + b + a * c + d - (e - e) > 30) | val calc1
(a b c d e: int)
(h1: (unit -> Lemma (a * (c + 1) = 24)))
(h2: (unit -> Lemma (b + d == 25)))
: Lemma (a + b + a * c + d - (e - e) > 30) | let calc1 (a b c d e : int)
(h1 : unit -> Lemma (a * (c + 1) = 24))
(h2 : unit -> Lemma (b + d == 25))
: Lemma (a + b + a * c + d - (e - e) > 30) =
calc (>) {
a + b + a * c + d - (e - e);
== { (* reorder, e-e = 0 *) }
a + a * c + b + d;
== { (* distributivity *) }
a * (c + 1) + b + d;
== { h1 () }
24 + b + d;
== { h2 () }
24 + 25;
== { }
49;
> { }
30;
} | {
"file_name": "examples/calc/CalcTest.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 46,
"start_col": 0,
"start_line": 28
} | module CalcTest
open FStar.Mul
open FStar.Calc
let lem1 (a : pos) : Lemma (2 * a > a) = ()
let calc0 (a : pos) : Lemma (a + a > a) =
calc (>) {
a + a;
== {}
2 * a;
> { lem1 a }
a;
}
(* The lemma above, desugared. F* eta-expands and ascribes <: Type in
* order to treat boolean operators such as (>) as `relation`s. *)
let calc0_desugared (a : pos) : Lemma (a + a > a) =
calc_finish (fun x y -> (>) x y <: Type) (fun () ->
calc_step (fun x y -> (>) x y <: Type) a (fun () ->
calc_step (fun x y -> (==) x y <: Type) (2 * a) (fun () ->
calc_init (a + a)
) (fun () -> ())
) (fun () -> lem1 a)
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "CalcTest.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Prims.int ->
b: Prims.int ->
c: Prims.int ->
d: Prims.int ->
e: Prims.int ->
h1: (_: Prims.unit -> FStar.Pervasives.Lemma (ensures a * (c + 1) = 24)) ->
h2: (_: Prims.unit -> FStar.Pervasives.Lemma (ensures b + d == 25))
-> FStar.Pervasives.Lemma (ensures a + b + a * c + d - (e - e) > 30) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.int",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.b2t",
"Prims.op_Equality",
"FStar.Mul.op_Star",
"Prims.op_Addition",
"Prims.Nil",
"FStar.Pervasives.pattern",
"Prims.eq2",
"FStar.Calc.calc_finish",
"Prims.op_GreaterThan",
"Prims.op_Subtraction",
"Prims.Cons",
"FStar.Preorder.relation",
"FStar.Calc.calc_step",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack"
] | [] | false | false | true | false | false | let calc1
(a b c d e: int)
(h1: (unit -> Lemma (a * (c + 1) = 24)))
(h2: (unit -> Lemma (b + d == 25)))
: Lemma (a + b + a * c + d - (e - e) > 30) =
| calc ( > ) {
a + b + a * c + d - (e - e);
( == ) { () }
a + a * c + b + d;
( == ) { () }
a * (c + 1) + b + d;
( == ) { h1 () }
24 + b + d;
( == ) { h2 () }
24 + 25;
( == ) { () }
49;
( > ) { () }
30;
} | false |
LowStar.Vector.fst | LowStar.Vector.forall2_all | val forall2_all:
#a:Type -> h:HS.mem -> vec:vector a ->
p:(a -> a -> GTot Type0) -> GTot Type0 | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 35,
"end_line": 603,
"start_col": 0,
"start_line": 602
} | (*
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 -> | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
h: FStar.Monotonic.HyperStack.mem ->
vec: LowStar.Vector.vector a ->
p: (_: a -> _: a -> Prims.GTot Type0)
-> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"LowStar.Vector.vector",
"LowStar.Vector.forall2",
"FStar.UInt32.__uint_to_t",
"LowStar.Vector.size_of"
] | [] | false | false | false | false | true | let forall2_all #a h vec p =
| forall2 h vec 0ul (size_of vec) p | false |
LowStar.Vector.fst | LowStar.Vector.get_preserved_within | 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)] | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 643,
"start_col": 0,
"start_line": 635
} | (*
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); | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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)
] | FStar.Pervasives.Lemma | [
"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"
] | [] | false | false | true | false | false | let get_preserved_within #a vec i j k h0 h1 =
| 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) | false |
LowStar.Vector.fst | LowStar.Vector.modifies_as_seq_within | 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)] | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 258,
"start_col": 0,
"start_line": 247
} | (*
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); | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
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)
] | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | false | let rec modifies_as_seq_within #a vec i j dloc h0 h1 =
| 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))))) | false |
Ast.fst | Ast.string_of_pos | val string_of_pos : p: Ast.pos -> Prims.string | let string_of_pos p =
Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 53,
"end_line": 77,
"start_col": 0,
"start_line": 76
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Ast.pos -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Ast.pos",
"FStar.Printf.sprintf",
"Ast.__proj__Mkpos__item__filename",
"Ast.__proj__Mkpos__item__line",
"Ast.__proj__Mkpos__item__col",
"Prims.string"
] | [] | false | false | false | true | false | let string_of_pos p =
| Printf.sprintf "%s:(%d,%d)" p.filename p.line p.col | false |
|
Ast.fst | Ast.string_of_range | val string_of_range : r: (Ast.pos * Ast.pos) -> Prims.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) | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 31,
"end_line": 92,
"start_col": 0,
"start_line": 85
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: (Ast.pos * Ast.pos) -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple2",
"Ast.pos",
"Prims.op_Equality",
"Prims.string",
"Ast.__proj__Mkpos__item__filename",
"FStar.Printf.sprintf",
"Ast.__proj__Mkpos__item__line",
"Ast.__proj__Mkpos__item__col",
"Prims.bool",
"Ast.string_of_pos"
] | [] | false | false | false | true | false | 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 |
|
Ast.fst | Ast.range | val range : Type0 | let range = pos * pos | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 21,
"end_line": 80,
"start_col": 0,
"start_line": 80
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple2",
"Ast.pos"
] | [] | false | false | false | true | true | let range =
| pos * pos | false |
|
LowStar.Vector.fst | LowStar.Vector.get_preserved | 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)) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 622,
"start_col": 0,
"start_line": 620
} | (*
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)) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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) | FStar.Pervasives.Lemma | [
"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"
] | [] | true | false | true | false | 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 | false |
Ast.fst | Ast.reserved_prefix | val reserved_prefix : Prims.string | let reserved_prefix = "___" | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 27,
"end_line": 21,
"start_col": 0,
"start_line": 21
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.string | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let reserved_prefix =
| "___" | false |
|
Ast.fst | Ast.with_range | val with_range (x: 'a) (r: range) : with_meta_t 'a | val with_range (x: 'a) (r: range) : with_meta_t 'a | let with_range (x:'a) (r:range) : with_meta_t 'a = with_range_and_comments x r [] | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 81,
"end_line": 119,
"start_col": 0,
"start_line": 119
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: 'a -> r: Ast.range -> Ast.with_meta_t 'a | Prims.Tot | [
"total"
] | [] | [
"Ast.range",
"Ast.with_range_and_comments",
"Prims.Nil",
"Prims.string",
"Ast.with_meta_t"
] | [] | false | false | false | true | false | let with_range (x: 'a) (r: range) : with_meta_t 'a =
| with_range_and_comments x r [] | false |
Ast.fst | Ast.with_meta_t_of_yojson | val with_meta_t_of_yojson (#a #b #c: Type) (f: (a -> b)) (x: a) : ML c | val with_meta_t_of_yojson (#a #b #c: Type) (f: (a -> b)) (x: a) : ML c | let with_meta_t_of_yojson (#a #b #c:Type) (f:(a -> b)) (x:a)
: ML c
= failwith "No reading yojson" | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 32,
"end_line": 110,
"start_col": 0,
"start_line": 108
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> b) -> x: a -> FStar.All.ML c | FStar.All.ML | [
"ml"
] | [] | [
"FStar.All.failwith"
] | [] | false | true | false | false | false | let with_meta_t_of_yojson (#a #b #c: Type) (f: (a -> b)) (x: a) : ML c =
| failwith "No reading yojson" | false |
Ast.fst | Ast.comments | val comments : Type0 | let comments = list string | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 26,
"end_line": 83,
"start_col": 0,
"start_line": 83
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Prims.string"
] | [] | false | false | false | true | true | let comments =
| list string | false |
|
Ast.fst | Ast.dummy_pos | val dummy_pos : Ast.pos | let dummy_pos = {
filename="";
line=0;
col=0;
} | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 98,
"start_col": 0,
"start_line": 94
} | (*
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) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Ast.pos | Prims.Tot | [
"total"
] | [] | [
"Ast.Mkpos"
] | [] | false | false | false | true | false | let dummy_pos =
| { filename = ""; line = 0; col = 0 } | false |
|
Ast.fst | Ast.with_meta_t_to_yojson | val with_meta_t_to_yojson (f: ('a -> 'b)) (x: with_meta_t 'a) : 'b | 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
= f x.v | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 9,
"end_line": 113,
"start_col": 0,
"start_line": 111
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: 'a -> 'b) -> x: Ast.with_meta_t 'a -> 'b | Prims.Tot | [
"total"
] | [] | [
"Ast.with_meta_t",
"Ast.__proj__Mkwith_meta_t__item__v"
] | [] | false | false | false | true | false | let with_meta_t_to_yojson (f: ('a -> 'b)) (x: with_meta_t 'a) : 'b =
| f x.v | false |
Ast.fst | Ast.ident_name | val ident_name : i: Ast.with_meta_t Ast.ident' -> Prims.string | let ident_name i = i.v.name | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 27,
"end_line": 135,
"start_col": 0,
"start_line": 135
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: Ast.with_meta_t Ast.ident' -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Ast.with_meta_t",
"Ast.ident'",
"Ast.__proj__Mkident'__item__name",
"Ast.__proj__Mkwith_meta_t__item__v",
"Prims.string"
] | [] | false | false | false | true | false | let ident_name i =
| i.v.name | false |
|
LowStar.Vector.fst | LowStar.Vector.flush | 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))))) | 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 | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 17,
"end_line": 503,
"start_col": 0,
"start_line": 496
} | (*
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) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | 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) | FStar.HyperStack.ST.ST | [] | [] | [
"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"
] | [] | false | true | false | false | 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 | false |
Ast.fst | Ast.ident | val ident : Type0 | let ident = with_meta_t ident' | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 30,
"end_line": 127,
"start_col": 0,
"start_line": 127
} | (*
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
} | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Ast.with_meta_t",
"Ast.ident'"
] | [] | false | false | false | true | true | let ident =
| with_meta_t ident' | false |
|
LowStar.Vector.fst | LowStar.Vector.forall_preserved | 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)) | 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 =
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)) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 53,
"end_line": 690,
"start_col": 0,
"start_line": 687
} | (*
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)) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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) | FStar.Pervasives.Lemma | [
"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"
] | [] | true | false | true | false | 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)) | false |
LowStar.Vector.fst | LowStar.Vector.assign | 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)) | 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 =
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)) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 89,
"end_line": 435,
"start_col": 0,
"start_line": 420
} | (*
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)) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | vec: LowStar.Vector.vector a -> i: LowStar.Vector.uint32_t -> v: a
-> FStar.HyperStack.ST.ST Prims.unit | FStar.HyperStack.ST.ST | [] | [] | [
"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"
] | [] | false | true | false | false | false | let assign #a vec i v =
| 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)) | false |
Ast.fst | Ast.field_typ | val field_typ : Type0 | let field_typ = t:typ { Type_app? t.v } | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 39,
"end_line": 369,
"start_col": 0,
"start_line": 369
} | (*
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' | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Ast.typ",
"Prims.b2t",
"Ast.uu___is_Type_app",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.typ'"
] | [] | false | false | false | true | true | let field_typ =
| t: typ{Type_app? t.v} | false |
|
LowStar.Vector.fst | LowStar.Vector.forall_as_seq | 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)] | 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)) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 42,
"end_line": 675,
"start_col": 0,
"start_line": 673
} | (*
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); | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false |
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)
] | FStar.Pervasives.Lemma | [
"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"
] | [] | true | false | true | false | 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)) | false |
Ast.fst | Ast.enum_case | val enum_case : Type0 | let enum_case = ident & option (either int ident) | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 49,
"end_line": 530,
"start_col": 0,
"start_line": 530
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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
} | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple2",
"Ast.ident",
"FStar.Pervasives.Native.option",
"Ast.either",
"Prims.int"
] | [] | false | false | false | true | true | let enum_case =
| ident & option (either int ident) | false |
|
Ast.fst | Ast.field_bitwidth_t | val field_bitwidth_t : Type0 | let field_bitwidth_t = either (with_meta_t int) bitfield_attr | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 61,
"end_line": 454,
"start_col": 0,
"start_line": 454
} | (*
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' | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Ast.either",
"Ast.with_meta_t",
"Prims.int",
"Ast.bitfield_attr"
] | [] | false | false | false | true | true | let field_bitwidth_t =
| either (with_meta_t int) bitfield_attr | false |
|
Ast.fst | Ast.ident_to_string | val ident_to_string : i: Ast.with_meta_t Ast.ident' -> Prims.string | let ident_to_string i = Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 10,
"end_line": 133,
"start_col": 0,
"start_line": 129
} | (*
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' | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: Ast.with_meta_t Ast.ident' -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Ast.with_meta_t",
"Ast.ident'",
"FStar.Printf.sprintf",
"Ast.__proj__Mkident'__item__modul_name",
"Ast.__proj__Mkwith_meta_t__item__v",
"Prims.string",
"Prims.op_Hat",
"Ast.__proj__Mkident'__item__name"
] | [] | false | false | false | true | false | let ident_to_string i =
| Printf.sprintf "%s%s"
(match i.v.modul_name with
| None -> ""
| Some m -> m ^ ".")
i.v.name | false |
|
Ast.fst | Ast.prog | val prog : Type0 | let prog = list decl & option type_refinement | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 45,
"end_line": 601,
"start_col": 0,
"start_line": 601
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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)
} | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple2",
"Prims.list",
"Ast.decl",
"FStar.Pervasives.Native.option",
"Ast.type_refinement"
] | [] | false | false | false | true | true | let prog =
| list decl & option type_refinement | false |
|
Ast.fst | Ast.decl_with_v | val decl_with_v (d: decl) (v: decl') : decl | val decl_with_v (d: decl) (v: decl') : decl | let decl_with_v (d:decl) (v:decl') : decl =
{ d with d_decl = { d.d_decl with v = v } } | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 45,
"end_line": 591,
"start_col": 0,
"start_line": 590
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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 } | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | d: Ast.decl -> v: Ast.decl' -> Ast.decl | Prims.Tot | [
"total"
] | [] | [
"Ast.decl",
"Ast.decl'",
"Ast.Mkdecl",
"Ast.Mkwith_meta_t",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.__proj__Mkwith_meta_t__item__comments",
"Ast.with_meta_t",
"Ast.__proj__Mkdecl__item__d_decl",
"Ast.__proj__Mkdecl__item__d_exported"
] | [] | false | false | false | true | false | let decl_with_v (d: decl) (v: decl') : decl =
| { d with d_decl = { d.d_decl with v = v } } | false |
LowStar.Vector.fst | LowStar.Vector.insert | 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))) | 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 =
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) | {
"file_name": "ulib/LowStar.Vector.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 26,
"end_line": 475,
"start_col": 0,
"start_line": 462
} | (*
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))) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | vec: LowStar.Vector.vector a -> v: a -> FStar.HyperStack.ST.ST (LowStar.Vector.vector a) | FStar.HyperStack.ST.ST | [] | [] | [
"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"
] | [] | false | true | false | false | 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) | false |
Ast.fst | Ast.has_entrypoint | val has_entrypoint (l: list attribute) : Tot bool | val has_entrypoint (l: list attribute) : Tot bool | let has_entrypoint (l:list attribute) : Tot bool =
Some? (List.Tot.tryFind (function Entrypoint -> true | _ -> false) l) | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 71,
"end_line": 610,
"start_col": 0,
"start_line": 609
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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 *) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list Ast.attribute -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Ast.attribute",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.List.Tot.Base.tryFind",
"Prims.bool"
] | [] | false | false | false | true | false | let has_entrypoint (l: list attribute) : Tot bool =
| Some? (List.Tot.tryFind (function
| Entrypoint -> true
| _ -> false)
l) | false |
Ast.fst | Ast.is_entrypoint_or_export | val is_entrypoint_or_export : d: Ast.decl -> Prims.bool | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 21,
"end_line": 618,
"start_col": 0,
"start_line": 612
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | d: Ast.decl -> Prims.bool | Prims.Tot | [
"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.has_entrypoint",
"Ast.__proj__Mktypedef_names__item__typedef_attributes",
"Prims.bool",
"Ast.__proj__Mkdecl__item__d_exported",
"Ast.switch_case"
] | [] | false | false | false | true | false | 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 | false |
|
Ast.fst | Ast.is_entrypoint | val is_entrypoint : d: Ast.decl -> Prims.bool | let is_entrypoint d = match d.d_decl.v with
| Record names _ _ _
| CaseType names _ _ ->
has_entrypoint (names.typedef_attributes)
| _ -> false | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 14,
"end_line": 624,
"start_col": 0,
"start_line": 620
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | d: Ast.decl -> Prims.bool | Prims.Tot | [
"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.has_entrypoint",
"Ast.__proj__Mktypedef_names__item__typedef_attributes",
"Ast.switch_case",
"Prims.bool"
] | [] | false | false | false | true | false | let is_entrypoint d =
| match d.d_decl.v with
| Record names _ _ _ | CaseType names _ _ -> has_entrypoint (names.typedef_attributes)
| _ -> false | false |
|
FStar.Seq.Properties.fst | FStar.Seq.Properties.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)) (decreases (List.Tot.length l)) | 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))
= fun #_ i s l ->
match l with
| [] -> ()
| hd :: tl ->
lemma_seq_of_list_induction l;
elim_of_list'' (i + 1) s tl | {
"file_name": "ulib/FStar.Seq.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 33,
"end_line": 588,
"start_col": 0,
"start_line": 570
} | (*
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
| [] -> ()
| hd::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 ->
assert (s `equal` cons (head s) (tail s));
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | 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.Base.seq_of_list l)
(ensures FStar.Seq.Properties.explode_and i s l)
(decreases FStar.List.Tot.Base.length l) | FStar.Pervasives.Lemma | [
"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"
] | [
"recursion"
] | false | false | true | false | 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 | false |
Ast.fst | Ast.out_expr_is_out_type_expr | val out_expr_is_out_type_expr (lhs: out_expr) : Tot bool | val out_expr_is_out_type_expr (lhs: out_expr) : Tot bool | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 13,
"end_line": 634,
"start_col": 0,
"start_line": 631
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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?
*) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lhs: Ast.out_expr -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Ast.out_expr",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.out_expr'",
"Ast.__proj__Mkout_expr__item__out_expr_node",
"Ast.ident",
"Ast.range",
"Ast.comments",
"FStar.Pervasives.Native.option",
"Ast.out_expr_meta_t",
"Prims.bool"
] | [] | false | false | false | true | false | 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 | false |
Hacl.Impl.Ed25519.RecoverX.fst | Hacl.Impl.Ed25519.RecoverX.elemB | val elemB : Type0 | let elemB = lbuffer uint64 5ul | {
"file_name": "code/ed25519/Hacl.Impl.Ed25519.RecoverX.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 30,
"end_line": 21,
"start_col": 0,
"start_line": 21
} | 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" | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"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"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint64",
"FStar.UInt32.__uint_to_t"
] | [] | false | false | false | true | true | let elemB =
| lbuffer uint64 5ul | false |
|
Ast.fst | Ast.atomic_action_has_out_expr | val atomic_action_has_out_expr (a: atomic_action) : Tot bool | val atomic_action_has_out_expr (a: atomic_action) : Tot bool | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 14,
"end_line": 642,
"start_col": 0,
"start_line": 637
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Ast.atomic_action -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Ast.atomic_action",
"Ast.expr",
"Ast.out_expr",
"Ast.out_expr_is_out_type_expr",
"Prims.bool"
] | [] | false | false | false | true | false | 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 | false |
Ast.fst | Ast.atomic_field_has_out_expr | val atomic_field_has_out_expr (f: atomic_field) : Tot bool | val atomic_field_has_out_expr (f: atomic_field) : Tot bool | let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
let sf = f.v in
field_action_has_out_expr sf.field_action | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 43,
"end_line": 675,
"start_col": 0,
"start_line": 673
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: Ast.atomic_field -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Ast.atomic_field",
"Ast.field_action_has_out_expr",
"Ast.__proj__Mkatomic_field'__item__field_action",
"Ast.atomic_field'",
"Ast.__proj__Mkwith_meta_t__item__v",
"Prims.bool"
] | [] | false | false | false | true | false | let atomic_field_has_out_expr (f: atomic_field) : Tot bool =
| let sf = f.v in
field_action_has_out_expr sf.field_action | false |
Ast.fst | Ast.comments_buffer | val comments_buffer:comments_buffer_t | val comments_buffer:comments_buffer_t | 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
} | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 73,
"start_col": 0,
"start_line": 44
} | (*
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);
} | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Ast.comments_buffer_t | Prims.Tot | [
"total"
] | [] | [
"Ast.Mkcomments_buffer_t",
"Ast.pos",
"Prims.list",
"Prims.string",
"FStar.Pervasives.Native.tuple3",
"FStar.List.map",
"Prims.unit",
"FStar.ST.op_Colon_Equals",
"FStar.Heap.trivial_preorder",
"FStar.Pervasives.Native.tuple2",
"FStar.List.partition",
"Prims.op_LessThanOrEqual",
"Ast.__proj__Mkpos__item__line",
"Prims.bool",
"Options.debug_print_string",
"FStar.Printf.sprintf",
"FStar.ST.op_Bang",
"FStar.Pervasives.all_post_h",
"FStar.Monotonic.Heap.heap",
"Prims.l_and",
"FStar.Monotonic.Heap.sel",
"Prims.Nil",
"Prims.l_Forall",
"Prims.l_imp",
"FStar.Monotonic.Heap.contains",
"FStar.Monotonic.Heap.modifies",
"FStar.Set.singleton",
"Prims.nat",
"FStar.Monotonic.Heap.addr_of",
"FStar.Monotonic.Heap.equal_dom",
"Prims.eq2",
"FStar.Pervasives.result",
"FStar.List.Tot.Base.rev",
"Prims.l_True",
"FStar.Pervasives.V",
"Prims.exn",
"FStar.Pervasives.E",
"FStar.Pervasives.Err",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple3",
"FStar.String.substring",
"Prims.op_Subtraction",
"FStar.String.length",
"Ast.comments_buffer_t",
"FStar.ST.ref",
"FStar.ST.alloc",
"FStar.ST.mref"
] | [] | false | false | false | true | false | 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 } | false |
Ast.fst | Ast.parse_int_suffix | val parse_int_suffix (i: string) : string * option integer_type | val parse_int_suffix (i: string) : string * option integer_type | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 16,
"end_line": 170,
"start_col": 0,
"start_line": 159
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: Prims.string -> Prims.string * FStar.Pervasives.Native.option Ast.integer_type | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Prims.op_GreaterThanOrEqual",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Pervasives.Native.option",
"Ast.integer_type",
"FStar.Pervasives.Native.Some",
"Ast.UInt8",
"Ast.UInt16",
"Ast.UInt32",
"Ast.UInt64",
"FStar.Pervasives.Native.None",
"FStar.Pervasives.Native.tuple2",
"Prims.b2t",
"Prims.op_Equality",
"Prims.nat",
"FStar.String.strlen",
"Prims.op_Subtraction",
"FStar.String.sub",
"Prims.bool",
"FStar.String.length"
] | [] | false | false | false | true | false | 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 | false |
Ast.fst | Ast.integer_type_lub | val integer_type_lub (t1 t2: integer_type) : Tot integer_type | val integer_type_lub (t1 t2: integer_type) : Tot integer_type | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 25,
"end_line": 190,
"start_col": 0,
"start_line": 182
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t1: Ast.integer_type -> t2: Ast.integer_type -> Ast.integer_type | Prims.Tot | [
"total"
] | [] | [
"Ast.integer_type",
"FStar.Pervasives.Native.Mktuple2",
"Ast.UInt64",
"Ast.UInt32",
"Ast.UInt16",
"Ast.UInt8"
] | [] | false | false | false | true | false | 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 | false |
Ast.fst | Ast.check_reserved_identifier | val check_reserved_identifier : i: Ast.ident -> FStar.All.ALL Prims.unit | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 60,
"end_line": 150,
"start_col": 0,
"start_line": 145
} | (*
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) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: Ast.ident -> FStar.All.ALL Prims.unit | FStar.All.ALL | [] | [] | [
"Ast.ident",
"Prims.op_AmpAmp",
"Prims.op_GreaterThanOrEqual",
"FStar.String.length",
"Prims.op_Equality",
"Prims.string",
"FStar.String.sub",
"Ast.reserved_prefix",
"Ast.error",
"Prims.unit",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.ident'",
"Prims.bool",
"Ast.__proj__Mkident'__item__name",
"Ast.__proj__Mkwith_meta_t__item__v"
] | [] | false | true | false | false | false | 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 | false |
|
Ast.fst | Ast.integer_type_leq | val integer_type_leq (t1 t2: integer_type) : bool | val integer_type_leq (t1 t2: integer_type) : bool | let integer_type_leq (t1 t2: integer_type) : bool =
integer_type_lub t1 t2 = t2 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 29,
"end_line": 193,
"start_col": 0,
"start_line": 192
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t1: Ast.integer_type -> t2: Ast.integer_type -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Ast.integer_type",
"Prims.op_Equality",
"Ast.integer_type_lub",
"Prims.bool"
] | [] | false | false | false | true | false | let integer_type_leq (t1 t2: integer_type) : bool =
| integer_type_lub t1 t2 = t2 | false |
Ast.fst | Ast.bit_order_of | val bit_order_of (i: ident) : ML bitfield_bit_order | val bit_order_of (i: ident) : ML bitfield_bit_order | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 15,
"end_line": 243,
"start_col": 0,
"start_line": 240
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: Ast.ident -> FStar.All.ML Ast.bitfield_bit_order | FStar.All.ML | [
"ml"
] | [] | [
"Ast.ident",
"Ast.maybe_bit_order_of",
"Ast.error",
"Ast.bitfield_bit_order",
"Prims.op_Hat",
"Ast.ident_to_string",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.ident'"
] | [] | false | true | false | false | false | 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 | false |
Hacl.Test.HMAC_DRBG.fst | Hacl.Test.HMAC_DRBG.test_many | val test_many : label: C.String.t ->
f: (_: a -> FStar.HyperStack.ST.Stack Prims.unit) ->
vec: Test.Lowstarize.lbuffer a
-> FStar.HyperStack.ST.STATE Prims.unit | let test_many #a (label:C.String.t)
(f:a -> Stack unit (fun _ -> True) (fun _ _ _ -> True)) (vec: L.lbuffer a)
=
C.String.print label;
C.String.(print !$"\n");
let L.LB len vs = vec in
let f (i:UInt32.t{0 <= v i /\ v i < v len}): Stack unit
(requires fun h -> True)
(ensures fun h0 _ h1 -> True)
=
let open LowStar.BufferOps in
B.recall vs;
LowStar.Printf.(printf "HMAC-DRBG Test %ul/%ul\n" (i +! 1ul) len done);
f vs.(i)
in
C.Loops.for 0ul len (fun _ _ -> True) f | {
"file_name": "code/tests/Hacl.Test.HMAC_DRBG.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 41,
"end_line": 130,
"start_col": 0,
"start_line": 115
} | module Hacl.Test.HMAC_DRBG
open FStar.HyperStack.ST
open Test.Lowstarize
open Lib.IntTypes
open Hacl.HMAC_DRBG
open Spec.HMAC_DRBG.Test.Vectors
module D = Spec.Hash.Definitions
module L = Test.Lowstarize
module B = LowStar.Buffer
#set-options "--fuel 0 --ifuel 0 --z3rlimit 100"
(* FStar.Reflection only supports up to 8-tuples *)
noextract
let vectors_tmp = List.Tot.map
(fun x -> x.a, h x.entropy_input, h x.nonce, h x.personalization_string,
h x.entropy_input_reseed, h x.additional_input_reseed,
(h x.additional_input_1, h x.additional_input_2),
h x.returned_bits)
test_vectors
%splice[vectors_low] (lowstarize_toplevel "vectors_tmp" "vectors_low")
// Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t
assume val declassify_uint8: squash (uint8 == UInt8.t)
let vec8 = L.lbuffer UInt8.t
let vector = D.hash_alg & vec8 & vec8 & vec8 & vec8 & vec8 & (vec8 & vec8) & vec8
// This could replace TestLib.compare_and_print
val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t
-> Stack bool
(requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len)
(ensures fun h0 _ h1 -> B.modifies B.loc_none h0 h1)
let compare_and_print b1 b2 len =
push_frame();
LowStar.Printf.(printf "Expected: %xuy\n" len b1 done);
LowStar.Printf.(printf "Computed: %xuy\n" len b2 done);
let b = Lib.ByteBuffer.lbytes_eq #len b1 b2 in
if b then
LowStar.Printf.(printf "PASS\n" done)
else
LowStar.Printf.(printf "FAIL\n" done);
pop_frame();
b
let test_one (vec:vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) =
let a,
LB entropy_input_len entropy_input,
LB nonce_len nonce,
LB personalization_string_len personalization_string,
LB entropy_input_reseed_len entropy_input_reseed,
LB additional_input_reseed_len additional_input_reseed,
(LB additional_input_1_len additional_input_1,
LB additional_input_2_len additional_input_2),
LB returned_bits_len returned_bits = vec
in
B.recall entropy_input;
B.recall nonce;
B.recall personalization_string;
B.recall entropy_input_reseed;
B.recall additional_input_reseed;
B.recall additional_input_1;
B.recall additional_input_2;
B.recall returned_bits;
// We need to check this at runtime because Low*-ized vectors don't carry any refinements
if not (Spec.HMAC_DRBG.is_supported_alg a &&
min_length a <=. entropy_input_len &&
entropy_input_len <=. max_length &&
min_length a /. 2ul <=. nonce_len &&
nonce_len <=. max_length &&
personalization_string_len <=. max_personalization_string_length &&
min_length a <=. entropy_input_reseed_len &&
entropy_input_reseed_len <=. max_length &&
additional_input_reseed_len <=. max_additional_input_length &&
additional_input_1_len <=. max_additional_input_length &&
additional_input_2_len <=. max_additional_input_length &&
0ul <. returned_bits_len &&
returned_bits_len <=. max_output_length)
then C.exit (-1l)
else
begin
push_frame();
let output = B.alloca (u8 0) returned_bits_len in
let st = alloca a in
instantiate a st
entropy_input_len entropy_input
nonce_len nonce
personalization_string_len personalization_string;
reseed a st
entropy_input_reseed_len entropy_input_reseed
additional_input_reseed_len additional_input_reseed;
let ok = generate a output st returned_bits_len
additional_input_1_len additional_input_1
in
if ok then
let ok = generate a output st returned_bits_len
additional_input_2_len additional_input_2
in
if ok then
let ok = compare_and_print returned_bits output returned_bits_len in
if ok then ()
else C.exit 1l
else C.exit 1l
else C.exit 1l;
pop_frame()
end | {
"checked_file": "/",
"dependencies": [
"Test.Lowstarize.fst.checked",
"Spec.HMAC_DRBG.Test.Vectors.fst.checked",
"Spec.HMAC_DRBG.fsti.checked",
"Spec.Hash.Definitions.fst.checked",
"prims.fst.checked",
"LowStar.Printf.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Hacl.HMAC_DRBG.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Int32.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"C.String.fsti.checked",
"C.Loops.fst.checked",
"C.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Test.HMAC_DRBG.fst"
} | [
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "Test.Lowstarize",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Spec.Hash.Definitions",
"short_module": "D"
},
{
"abbrev": false,
"full_module": "Spec.HMAC_DRBG.Test.Vectors",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HMAC_DRBG",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Test.Lowstarize",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Test",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Test",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
label: C.String.t ->
f: (_: a -> FStar.HyperStack.ST.Stack Prims.unit) ->
vec: Test.Lowstarize.lbuffer a
-> FStar.HyperStack.ST.STATE Prims.unit | FStar.HyperStack.ST.STATE | [] | [] | [
"C.String.t",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_True",
"Test.Lowstarize.lbuffer",
"FStar.UInt32.t",
"LowStar.Buffer.buffer",
"Prims.l_and",
"Prims.eq2",
"LowStar.Monotonic.Buffer.len",
"LowStar.Buffer.trivial_preorder",
"LowStar.Monotonic.Buffer.recallable",
"C.Loops.for",
"FStar.UInt32.__uint_to_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Prims.op_LessThan",
"LowStar.BufferOps.op_Array_Access",
"LowStar.Printf.printf",
"Lib.IntTypes.op_Plus_Bang",
"LowStar.Printf.done",
"LowStar.Monotonic.Buffer.recall",
"C.String.print",
"C.String.op_Bang_Dollar"
] | [] | false | true | false | false | false | let test_many
#a
(label: C.String.t)
(f: (a -> Stack unit (fun _ -> True) (fun _ _ _ -> True)))
(vec: L.lbuffer a)
=
| C.String.print label;
(let open C.String in print !$"\n");
let L.LB len vs = vec in
let f (i: UInt32.t{0 <= v i /\ v i < v len})
: Stack unit (requires fun h -> True) (ensures fun h0 _ h1 -> True) =
let open LowStar.BufferOps in
B.recall vs;
(let open LowStar.Printf in printf "HMAC-DRBG Test %ul/%ul\n" (i +! 1ul) len done);
f vs.(i)
in
C.Loops.for 0ul len (fun _ _ -> True) f | false |
|
Ast.fst | Ast.maybe_as_integer_typ | val maybe_as_integer_typ (i: ident) : Tot (option integer_type) | val maybe_as_integer_typ (i: ident) : Tot (option integer_type) | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 15,
"end_line": 208,
"start_col": 0,
"start_line": 195
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: Ast.ident -> FStar.Pervasives.Native.option Ast.integer_type | Prims.Tot | [
"total"
] | [] | [
"Ast.ident",
"Prims.op_disEquality",
"FStar.Pervasives.Native.option",
"Prims.string",
"Ast.__proj__Mkident'__item__modul_name",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.ident'",
"FStar.Pervasives.Native.None",
"Ast.integer_type",
"Prims.bool",
"Ast.__proj__Mkident'__item__name",
"FStar.Pervasives.Native.Some",
"Ast.UInt8",
"Ast.UInt16",
"Ast.UInt32",
"Ast.UInt64"
] | [] | false | false | false | true | false | 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 | false |
Ast.fst | Ast.decl_has_out_expr | val decl_has_out_expr (d: decl) : Tot bool | val decl_has_out_expr (d: decl) : Tot bool | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 14,
"end_line": 721,
"start_col": 0,
"start_line": 715
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | d: Ast.decl -> Prims.bool | Prims.Tot | [
"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"
] | [] | false | false | false | true | 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 | false |
Ast.fst | Ast.as_integer_typ | val as_integer_typ (i: ident) : ML integer_type | val as_integer_typ (i: ident) : ML integer_type | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 15,
"end_line": 213,
"start_col": 0,
"start_line": 210
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: Ast.ident -> FStar.All.ML Ast.integer_type | FStar.All.ML | [
"ml"
] | [] | [
"Ast.ident",
"Ast.maybe_as_integer_typ",
"Ast.error",
"Ast.integer_type",
"Prims.op_Hat",
"Ast.ident_to_string",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.ident'"
] | [] | false | true | false | false | false | 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 | false |
Ast.fst | Ast.sequence_non_failing_actions | val sequence_non_failing_actions (a0: action{Action_act? a0.v}) (a1: action{Action_act? a1.v})
: a: action{Action_act? a.v} | val sequence_non_failing_actions (a0: action{Action_act? a0.v}) (a1: action{Action_act? a1.v})
: a: action{Action_act? a.v} | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 18,
"end_line": 429,
"start_col": 0,
"start_line": 395
} | (*
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 | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a0: Ast.action{Action_act? (Mkwith_meta_t?.v a0)} ->
a1: Ast.action{Action_act? (Mkwith_meta_t?.v a1)}
-> a: Ast.action{Action_act? (Mkwith_meta_t?.v a)} | Prims.Tot | [
"total"
] | [] | [
"Ast.action",
"Prims.b2t",
"Ast.uu___is_Action_act",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.action'",
"Ast.with_range_and_comments",
"Ast.Action_act",
"Ast.__proj__Mkwith_meta_t__item__range",
"Ast.__proj__Mkwith_meta_t__item__comments",
"Ast.with_meta_t",
"Ast.atomic_action",
"Ast.Action_seq",
"Ast.expr",
"FStar.Pervasives.Native.option",
"Ast.Action_ite",
"FStar.Pervasives.Native.Some",
"Ast.ident",
"Ast.Action_let",
"FStar.List.Tot.Base.op_At",
"Prims.string"
] | [] | false | false | false | false | false | 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 | false |
Ast.fst | Ast.maybe_bit_order_of | val maybe_bit_order_of (i: ident)
: Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y -> Some? y == Some? (maybe_as_integer_typ i))) | val maybe_bit_order_of (i: ident)
: Pure (option bitfield_bit_order)
(requires True)
(ensures (fun y -> Some? y == Some? (maybe_as_integer_typ i))) | 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 | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 15,
"end_line": 238,
"start_col": 0,
"start_line": 221
} | (*
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) *) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: Ast.ident -> Prims.Pure (FStar.Pervasives.Native.option Ast.bitfield_bit_order) | Prims.Pure | [] | [] | [
"Ast.ident",
"Prims.op_disEquality",
"FStar.Pervasives.Native.option",
"Prims.string",
"Ast.__proj__Mkident'__item__modul_name",
"Ast.__proj__Mkwith_meta_t__item__v",
"Ast.ident'",
"FStar.Pervasives.Native.None",
"Ast.bitfield_bit_order",
"Prims.bool",
"Ast.__proj__Mkident'__item__name",
"FStar.Pervasives.Native.Some",
"Ast.LSBFirst",
"Ast.MSBFirst",
"Prims.l_True",
"Prims.eq2",
"FStar.Pervasives.Native.uu___is_Some",
"Ast.integer_type",
"Ast.maybe_as_integer_typ"
] | [] | false | false | false | false | false | 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 | false |
Ast.fst | Ast.dummy_range | val dummy_range : Ast.pos * Ast.pos | let dummy_range = dummy_pos, dummy_pos | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 38,
"end_line": 781,
"start_col": 0,
"start_line": 781
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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
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
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Ast.pos * Ast.pos | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.Mktuple2",
"Ast.pos",
"Ast.dummy_pos"
] | [] | false | false | false | true | false | let dummy_range =
| dummy_pos, dummy_pos | false |
|
Ast.fst | Ast.eq_typ_param | val eq_typ_param (p1 p2: typ_param) : bool | val eq_typ_param (p1 p2: typ_param) : bool | let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 14,
"end_line": 762,
"start_col": 0,
"start_line": 758
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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
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
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p1: Ast.typ_param -> p2: Ast.typ_param -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Ast.typ_param",
"FStar.Pervasives.Native.Mktuple2",
"Ast.either",
"Ast.expr",
"Ast.out_expr",
"Ast.eq_expr",
"Ast.eq_out_expr",
"FStar.Pervasives.Native.tuple2",
"Prims.bool"
] | [] | false | false | false | true | false | let eq_typ_param (p1 p2: typ_param) : bool =
| match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false | false |
Ast.fst | Ast.puint8 | val puint8 : Ast.with_meta_t Ast.typ' | let puint8 = mk_prim_t "PUINT8" | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 31,
"end_line": 789,
"start_col": 0,
"start_line": 789
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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
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
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8" | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Ast.with_meta_t Ast.typ' | Prims.Tot | [
"total"
] | [] | [
"Ast.mk_prim_t"
] | [] | false | false | false | true | false | let puint8 =
| mk_prim_t "PUINT8" | false |
|
Ast.fst | Ast.tuint32 | val tuint32 : Ast.with_meta_t Ast.typ' | let tuint32 = mk_prim_t "UINT32" | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 32,
"end_line": 791,
"start_col": 0,
"start_line": 791
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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
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
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let tuint8be = mk_prim_t "UINT8BE"
let puint8 = mk_prim_t "PUINT8" | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Ast.with_meta_t Ast.typ' | Prims.Tot | [
"total"
] | [] | [
"Ast.mk_prim_t"
] | [] | false | false | false | true | false | let tuint32 =
| mk_prim_t "UINT32" | false |
|
Ast.fst | Ast.mk_prim_t | val mk_prim_t : x: Prims.string -> Ast.with_meta_t Ast.typ' | let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec []) | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 90,
"end_line": 784,
"start_col": 0,
"start_line": 784
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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
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
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Prims.string -> Ast.with_meta_t Ast.typ' | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Ast.with_dummy_range",
"Ast.typ'",
"Ast.Type_app",
"Ast.ident'",
"Ast.to_ident'",
"Ast.KindSpec",
"Prims.Nil",
"Ast.either",
"Ast.expr",
"Ast.out_expr",
"Ast.with_meta_t"
] | [] | false | false | false | true | false | let mk_prim_t x =
| with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec []) | false |
|
Ast.fst | Ast.tbool | val tbool : Ast.with_meta_t Ast.typ' | let tbool = mk_prim_t "Bool" | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 28,
"end_line": 785,
"start_col": 0,
"start_line": 785
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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
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
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x} | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Ast.with_meta_t Ast.typ' | Prims.Tot | [
"total"
] | [] | [
"Ast.mk_prim_t"
] | [] | false | false | false | true | false | let tbool =
| mk_prim_t "Bool" | false |
|
Ast.fst | Ast.tuint64 | val tuint64 : Ast.with_meta_t Ast.typ' | let tuint64 = mk_prim_t "UINT64" | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 32,
"end_line": 792,
"start_col": 0,
"start_line": 792
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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
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
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let tuint8be = mk_prim_t "UINT8BE"
let puint8 = mk_prim_t "PUINT8"
let tuint16 = mk_prim_t "UINT16" | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Ast.with_meta_t Ast.typ' | Prims.Tot | [
"total"
] | [] | [
"Ast.mk_prim_t"
] | [] | false | false | false | true | false | let tuint64 =
| mk_prim_t "UINT64" | false |
|
Ast.fst | Ast.tunit | val tunit : Ast.with_meta_t Ast.typ' | let tunit = mk_prim_t "unit" | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 28,
"end_line": 786,
"start_col": 0,
"start_line": 786
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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
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
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec []) | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Ast.with_meta_t Ast.typ' | Prims.Tot | [
"total"
] | [] | [
"Ast.mk_prim_t"
] | [] | false | false | false | true | false | let tunit =
| mk_prim_t "unit" | false |
|
Hacl.Test.HMAC_DRBG.fst | Hacl.Test.HMAC_DRBG.main | val main: Prims.unit -> St C.exit_code | val main: Prims.unit -> St C.exit_code | let main () : St C.exit_code =
test_many C.String.(!$"[HMAC_DRBG]") test_one vectors_low;
C.EXIT_SUCCESS | {
"file_name": "code/tests/Hacl.Test.HMAC_DRBG.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 16,
"end_line": 134,
"start_col": 0,
"start_line": 132
} | module Hacl.Test.HMAC_DRBG
open FStar.HyperStack.ST
open Test.Lowstarize
open Lib.IntTypes
open Hacl.HMAC_DRBG
open Spec.HMAC_DRBG.Test.Vectors
module D = Spec.Hash.Definitions
module L = Test.Lowstarize
module B = LowStar.Buffer
#set-options "--fuel 0 --ifuel 0 --z3rlimit 100"
(* FStar.Reflection only supports up to 8-tuples *)
noextract
let vectors_tmp = List.Tot.map
(fun x -> x.a, h x.entropy_input, h x.nonce, h x.personalization_string,
h x.entropy_input_reseed, h x.additional_input_reseed,
(h x.additional_input_1, h x.additional_input_2),
h x.returned_bits)
test_vectors
%splice[vectors_low] (lowstarize_toplevel "vectors_tmp" "vectors_low")
// Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t
assume val declassify_uint8: squash (uint8 == UInt8.t)
let vec8 = L.lbuffer UInt8.t
let vector = D.hash_alg & vec8 & vec8 & vec8 & vec8 & vec8 & (vec8 & vec8) & vec8
// This could replace TestLib.compare_and_print
val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t
-> Stack bool
(requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len)
(ensures fun h0 _ h1 -> B.modifies B.loc_none h0 h1)
let compare_and_print b1 b2 len =
push_frame();
LowStar.Printf.(printf "Expected: %xuy\n" len b1 done);
LowStar.Printf.(printf "Computed: %xuy\n" len b2 done);
let b = Lib.ByteBuffer.lbytes_eq #len b1 b2 in
if b then
LowStar.Printf.(printf "PASS\n" done)
else
LowStar.Printf.(printf "FAIL\n" done);
pop_frame();
b
let test_one (vec:vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) =
let a,
LB entropy_input_len entropy_input,
LB nonce_len nonce,
LB personalization_string_len personalization_string,
LB entropy_input_reseed_len entropy_input_reseed,
LB additional_input_reseed_len additional_input_reseed,
(LB additional_input_1_len additional_input_1,
LB additional_input_2_len additional_input_2),
LB returned_bits_len returned_bits = vec
in
B.recall entropy_input;
B.recall nonce;
B.recall personalization_string;
B.recall entropy_input_reseed;
B.recall additional_input_reseed;
B.recall additional_input_1;
B.recall additional_input_2;
B.recall returned_bits;
// We need to check this at runtime because Low*-ized vectors don't carry any refinements
if not (Spec.HMAC_DRBG.is_supported_alg a &&
min_length a <=. entropy_input_len &&
entropy_input_len <=. max_length &&
min_length a /. 2ul <=. nonce_len &&
nonce_len <=. max_length &&
personalization_string_len <=. max_personalization_string_length &&
min_length a <=. entropy_input_reseed_len &&
entropy_input_reseed_len <=. max_length &&
additional_input_reseed_len <=. max_additional_input_length &&
additional_input_1_len <=. max_additional_input_length &&
additional_input_2_len <=. max_additional_input_length &&
0ul <. returned_bits_len &&
returned_bits_len <=. max_output_length)
then C.exit (-1l)
else
begin
push_frame();
let output = B.alloca (u8 0) returned_bits_len in
let st = alloca a in
instantiate a st
entropy_input_len entropy_input
nonce_len nonce
personalization_string_len personalization_string;
reseed a st
entropy_input_reseed_len entropy_input_reseed
additional_input_reseed_len additional_input_reseed;
let ok = generate a output st returned_bits_len
additional_input_1_len additional_input_1
in
if ok then
let ok = generate a output st returned_bits_len
additional_input_2_len additional_input_2
in
if ok then
let ok = compare_and_print returned_bits output returned_bits_len in
if ok then ()
else C.exit 1l
else C.exit 1l
else C.exit 1l;
pop_frame()
end
inline_for_extraction noextract
let test_many #a (label:C.String.t)
(f:a -> Stack unit (fun _ -> True) (fun _ _ _ -> True)) (vec: L.lbuffer a)
=
C.String.print label;
C.String.(print !$"\n");
let L.LB len vs = vec in
let f (i:UInt32.t{0 <= v i /\ v i < v len}): Stack unit
(requires fun h -> True)
(ensures fun h0 _ h1 -> True)
=
let open LowStar.BufferOps in
B.recall vs;
LowStar.Printf.(printf "HMAC-DRBG Test %ul/%ul\n" (i +! 1ul) len done);
f vs.(i)
in
C.Loops.for 0ul len (fun _ _ -> True) f | {
"checked_file": "/",
"dependencies": [
"Test.Lowstarize.fst.checked",
"Spec.HMAC_DRBG.Test.Vectors.fst.checked",
"Spec.HMAC_DRBG.fsti.checked",
"Spec.Hash.Definitions.fst.checked",
"prims.fst.checked",
"LowStar.Printf.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Hacl.HMAC_DRBG.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.Int32.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"C.String.fsti.checked",
"C.Loops.fst.checked",
"C.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Test.HMAC_DRBG.fst"
} | [
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "Test.Lowstarize",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "Spec.Hash.Definitions",
"short_module": "D"
},
{
"abbrev": false,
"full_module": "Spec.HMAC_DRBG.Test.Vectors",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.HMAC_DRBG",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Test.Lowstarize",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Test",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Test",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> FStar.HyperStack.ST.St C.exit_code | FStar.HyperStack.ST.St | [] | [] | [
"Prims.unit",
"C.EXIT_SUCCESS",
"C.exit_code",
"Hacl.Test.HMAC_DRBG.test_many",
"FStar.Pervasives.Native.tuple8",
"Spec.Hash.Definitions.hash_alg",
"Test.Lowstarize.lbuffer",
"FStar.UInt8.t",
"FStar.Pervasives.Native.tuple2",
"C.String.op_Bang_Dollar",
"Hacl.Test.HMAC_DRBG.test_one",
"Hacl.Test.HMAC_DRBG.vectors_low"
] | [] | false | true | false | false | false | let main () : St C.exit_code =
| test_many C.String.(!$"[HMAC_DRBG]") test_one vectors_low;
C.EXIT_SUCCESS | false |
Ast.fst | Ast.tuint16 | val tuint16 : Ast.with_meta_t Ast.typ' | let tuint16 = mk_prim_t "UINT16" | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 32,
"end_line": 790,
"start_col": 0,
"start_line": 790
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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
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
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit"
let tuint8 = mk_prim_t "UINT8"
let tuint8be = mk_prim_t "UINT8BE" | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Ast.with_meta_t Ast.typ' | Prims.Tot | [
"total"
] | [] | [
"Ast.mk_prim_t"
] | [] | false | false | false | true | false | let tuint16 =
| mk_prim_t "UINT16" | false |
|
Ast.fst | Ast.tuint8be | val tuint8be : Ast.with_meta_t Ast.typ' | let tuint8be = mk_prim_t "UINT8BE" | {
"file_name": "src/3d/Ast.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 34,
"end_line": 788,
"start_col": 0,
"start_line": 788
} | (*
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-single-element-array-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)
| FieldConsumeAll // [:consume-all]
[@@ PpxDerivingYoJson ]
noeq
type probe_call = {
probe_fn:option ident;
probe_length:expr;
probe_dest:ident
}
[@@ 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); //bool indicates if the action depends on the field value
field_probe:option probe_call; //set in case this field has to be probed then validated
}
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'
| ExternProbe : ident -> 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
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
(** Equality on expressions and types **)
/// eq_expr partially decides equality on expressions, by requiring
/// syntactic equality
let rec eq_expr (e1 e2:expr) : Tot bool (decreases e1) =
match e1.v, e2.v with
| Constant i, Constant j -> i = j
| Identifier i, Identifier j -> i.v = j.v
| This, This -> true
| App op1 es1, App op2 es2 ->
op1 = op2
&& eq_exprs es1 es2
| _ -> false
and eq_exprs (es1 es2:list expr) : Tot bool =
match es1, es2 with
| [], [] -> true
| hd1::es1, hd2::es2 -> eq_expr hd1 hd2 && eq_exprs es1 es2
| _ -> false
let eq_idents (i1 i2:ident) : Tot bool =
i1.v.modul_name = i2.v.modul_name && i1.v.name = i2.v.name
/// eq_typ: syntactic equalty of types
let rec eq_out_expr (o1 o2:out_expr) : bool =
match o1.out_expr_node.v, o2.out_expr_node.v with
| OE_id i1, OE_id i2 -> eq_idents i1 i2
| OE_star o1, OE_star o2
| OE_addrof o1, OE_addrof o2 -> eq_out_expr o1 o2
| OE_deref o1 i1, OE_deref o2 i2
| OE_dot o1 i1, OE_dot o2 i2 -> eq_idents i1 i2 && eq_out_expr o1 o2
| _ -> false
let eq_typ_param (p1 p2:typ_param) : bool =
match p1, p2 with
| Inl e1, Inl e2 -> eq_expr e1 e2
| Inr o1, Inr o2 -> eq_out_expr o1 o2
| _ -> false
let rec eq_typ_params (ps1 ps2:list typ_param) : bool =
match ps1, ps2 with
| [], [] -> true
| p1::ps1, p2::ps2 -> eq_typ_param p1 p2 && eq_typ_params ps1 ps2
| _ -> false
let rec eq_typ (t1 t2:typ) : Tot bool =
match t1.v, t2.v with
| Type_app hd1 k1 ps1, Type_app hd2 k2 ps2 ->
eq_idents hd1 hd2
&& k1 = k2
&& eq_typ_params ps1 ps2
| Pointer t1, Pointer t2 ->
eq_typ t1 t2
| _ -> false
(** Common AST constants and builders **)
let dummy_range = dummy_pos, dummy_pos
let with_dummy_range x = with_range x dummy_range
let to_ident' x = {modul_name=None;name=x}
let mk_prim_t x = with_dummy_range (Type_app (with_dummy_range (to_ident' x)) KindSpec [])
let tbool = mk_prim_t "Bool"
let tunit = mk_prim_t "unit" | {
"checked_file": "/",
"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"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Ast.with_meta_t Ast.typ' | Prims.Tot | [
"total"
] | [] | [
"Ast.mk_prim_t"
] | [] | false | false | false | true | false | let tuint8be =
| mk_prim_t "UINT8BE" | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.