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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
FStar.FiniteMap.Base.fsti | FStar.FiniteMap.Base.glue_elements_fact | val glue_elements_fact : Prims.logical | let glue_elements_fact =
forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).{:pattern elements (glue keys f)}
domain (glue keys f) == keys
/\ elements (glue keys f) == f | {
"file_name": "ulib/FStar.FiniteMap.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 311,
"start_col": 0,
"start_line": 308
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite maps as they're modeled in Dafny.
@summary Type and functions for modeling finite maps
*)
module FStar.FiniteMap.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
module FSet = FStar.FiniteSet.Base
type setfun_t (a: eqtype)
(b: Type u#b)
(s: FSet.set a) =
f: (a ^-> option b){forall (key: a). FSet.mem key s == Some? (f key)}
val map (a: eqtype) ([@@@ strictly_positive] b: Type u#b)
: Type u#b
(**
We translate each Dafny sequence function prefixed with `Map#`
into an F* function.
**)
/// We represent the Dafny function `Map#Domain` with `domain`:
///
/// function Map#Domain<U,V>(Map U V) : Set U;
val domain (#a: eqtype) (#b: Type u#b) (m: map a b)
: FSet.set a
/// We represent the Dafny function `Map#Elements` with `elements`:
///
/// function Map#Elements<U,V>(Map U V) : [U]V;
val elements (#a: eqtype) (#b: Type u#b) (m: map a b)
: setfun_t a b (domain m)
/// We represent the Dafny operator `in` on maps with `mem`:
let mem (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b) =
FSet.mem key (domain m)
/// We can convert a map to a list of pairs with `map_as_list`:
let rec key_in_item_list (#a: eqtype) (#b: Type u#b) (key: a) (items: list (a * b)) : bool =
match items with
| [] -> false
| (k, v) :: tl -> key = k || key_in_item_list key tl
let rec item_list_doesnt_repeat_keys (#a: eqtype) (#b: Type u#b) (items: list (a * b)) : bool =
match items with
| [] -> true
| (k, v) :: tl -> not (key_in_item_list k tl) && item_list_doesnt_repeat_keys tl
val map_as_list (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot (items: list (a * b){item_list_doesnt_repeat_keys items /\ (forall key. key_in_item_list key items <==> mem key m)})
/// We represent the Dafny operator [] on maps with `lookup`:
let lookup (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b{mem key m})
: b =
Some?.v ((elements m) key)
/// We represent the Dafny function `Map#Card` with `cardinality`:
///
/// function Map#Card<U,V>(Map U V) : int;
val cardinality (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot nat
/// We represent the Dafny function `Map#Values` with `values`:
///
/// function Map#Values<U,V>(Map U V) : Set V;
val values (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot (b -> prop)
/// We represent the Dafny function `Map#Items` with `items`:
///
/// function Map#Items<U,V>(Map U V) : Set Box;
val items (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot ((a * b) -> prop)
/// We represent the Dafny function `Map#Empty` with `emptymap`:
///
/// function Map#Empty<U, V>(): Map U V;
val emptymap (#a: eqtype) (#b: Type u#b)
: map a b
/// We represent the Dafny function `Map#Glue` with `glue`.
///
/// function Map#Glue<U, V>([U]bool, [U]V, Ty): Map U V;
val glue (#a: eqtype) (#b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys)
: map a b
/// We represent the Dafny function `Map#Build` with `insert`:
///
/// function Map#Build<U, V>(Map U V, U, V): Map U V;
val insert (#a: eqtype) (#b: Type u#b) (k: a) (v: b) (m: map a b)
: map a b
/// We represent the Dafny function `Map#Merge` with `merge`:
///
/// function Map#Merge<U, V>(Map U V, Map U V): Map U V;
val merge (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: map a b
/// We represent the Dafny function `Map#Subtract` with `subtract`:
///
/// function Map#Subtract<U, V>(Map U V, Set U): Map U V;
val subtract (#a: eqtype) (#b: Type u#b) (m: map a b) (s: FSet.set a)
: map a b
/// We represent the Dafny function `Map#Equal` with `equal`:
///
/// function Map#Equal<U, V>(Map U V, Map U V): bool;
val equal (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: prop
/// We represent the Dafny function `Map#Disjoint` with `disjoint`:
///
/// function Map#Disjoint<U, V>(Map U V, Map U V): bool;
val disjoint (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: prop
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (#b: Type u#b) (m: map a b{exists key. mem key m})
: GTot (key: a{mem key m})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b)
: map a b =
subtract m (FSet.singleton key)
let notin (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b)
: bool =
not (mem key m)
(**
We translate each finite map axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<U,V> m: Map U V :: { Map#Card(m) } 0 <= Map#Card(m));
/// We represent the following Dafny axiom with `cardinality_zero_iff_empty_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Card(m) }
/// Map#Card(m) == 0 <==> m == Map#Empty());
let cardinality_zero_iff_empty_fact =
forall (a: eqtype) (b:Type u#b) (m: map a b).{:pattern cardinality m}
cardinality m = 0 <==> m == emptymap
/// We represent the following Dafny axiom with `empty_or_domain_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Domain(m) }
/// m == Map#Empty() || (exists k: U :: Map#Domain(m)[k]));
let empty_or_domain_occupied_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern domain m}
m == emptymap \/ (exists k.{:pattern mem k m} mem k m)
/// We represent the following Dafny axiom with `empty_or_values_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Values(m) }
/// m == Map#Empty() || (exists v: V :: Map#Values(m)[v]));
let empty_or_values_occupied_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern values m}
m == emptymap \/ (exists v. {:pattern values m v } (values m) v)
/// We represent the following Dafny axiom with `empty_or_items_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Items(m) }
/// m == Map#Empty() || (exists k, v: Box :: Map#Items(m)[$Box(#_System._tuple#2._#Make2(k, v))]));
let empty_or_items_occupied_fact =
forall (a: eqtype) (b:Type u#b) (m: map a b).{:pattern items m}
m == emptymap \/ (exists item. {:pattern items m item } (items m) item)
/// We represent the following Dafny axiom with `map_cardinality_matches_domain_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Domain(m)) }
/// Set#Card(Map#Domain(m)) == Map#Card(m));
let map_cardinality_matches_domain_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern FSet.cardinality (domain m)}
FSet.cardinality (domain m) = cardinality m
/// We don't use the following Dafny axioms, which would require
/// treating the values and items as finite sets, which we can't do
/// because we want to allow non-eqtypes as values.
///
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Values(m)) }
/// Set#Card(Map#Values(m)) <= Map#Card(m));
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Items(m)) }
/// Set#Card(Map#Items(m)) == Map#Card(m));
/// We represent the following Dafny axiom with `values_contains_fact`:
///
/// axiom (forall<U,V> m: Map U V, v: V :: { Map#Values(m)[v] }
/// Map#Values(m)[v] ==
/// (exists u: U :: { Map#Domain(m)[u] } { Map#Elements(m)[u] }
/// Map#Domain(m)[u] &&
/// v == Map#Elements(m)[u]));
let values_contains_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (v: b).{:pattern (values m) v}
(values m) v <==>
(exists (u: a).{:pattern FSet.mem u (domain m) \/ ((elements m) u)}
FSet.mem u (domain m) /\ (elements m) u == Some v)
/// We represent the following Dafny axiom with `items_contains_fact`:
///
/// axiom (forall m: Map Box Box, item: Box :: { Map#Items(m)[item] }
/// Map#Items(m)[item] <==>
/// Map#Domain(m)[_System.Tuple2._0($Unbox(item))] &&
/// Map#Elements(m)[_System.Tuple2._0($Unbox(item))] == _System.Tuple2._1($Unbox(item)));
let items_contains_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (item: a * b).{:pattern (items m) item}
(items m) item <==>
FSet.mem (fst item) (domain m)
/\ (elements m) (fst item) == Some (snd item)
/// We represent the following Dafny axiom with `empty_domain_empty_fact`:
///
/// axiom (forall<U, V> u: U ::
/// { Map#Domain(Map#Empty(): Map U V)[u] }
/// !Map#Domain(Map#Empty(): Map U V)[u]);
let empty_domain_empty_fact =
forall (a: eqtype) (b: Type u#b) (u: a).{:pattern FSet.mem u (domain (emptymap #a #b))}
not (FSet.mem u (domain (emptymap #a #b)))
/// We represent the following Dafny axiom with `glue_domain_fact`:
///
/// axiom (forall<U, V> a: [U]bool, b: [U]V, t: Ty ::
/// { Map#Domain(Map#Glue(a, b, t)) }
/// Map#Domain(Map#Glue(a, b, t)) == a);
let glue_domain_fact =
forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).{:pattern domain (glue keys f)}
domain (glue keys f) == keys
/// We represent the following Dafny axiom with `glue_elements_fact`.
/// But we have to change it because our version of `Map#Elements`
/// returns a map to an optional value.
///
/// axiom (forall<U, V> a: [U]bool, b: [U]V, t: Ty ::
/// { Map#Elements(Map#Glue(a, b, t)) }
/// Map#Elements(Map#Glue(a, b, t)) == b); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.FiniteSet.Base.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteMap.Base.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FiniteSet.Base",
"short_module": "FSet"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteMap",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteMap",
"short_module": null
},
{
"abbrev": 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.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"FStar.FiniteMap.Base.setfun_t",
"Prims.l_and",
"Prims.eq2",
"FStar.FiniteMap.Base.domain",
"FStar.FiniteMap.Base.glue",
"FStar.FunctionalExtensionality.op_Hat_Subtraction_Greater",
"FStar.Pervasives.Native.option",
"Prims.l_or",
"Prims.bool",
"FStar.FiniteSet.Base.mem",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.FiniteMap.Base.elements"
] | [] | false | false | false | true | true | let glue_elements_fact =
| forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).
{:pattern elements (glue keys f)}
domain (glue keys f) == keys /\ elements (glue keys f) == f | false |
|
FStar.FiniteMap.Base.fsti | FStar.FiniteMap.Base.all_finite_map_facts | val all_finite_map_facts : Prims.logical | let all_finite_map_facts =
cardinality_zero_iff_empty_fact u#b
/\ empty_or_domain_occupied_fact u#b
/\ empty_or_values_occupied_fact u#b
/\ empty_or_items_occupied_fact u#b
/\ map_cardinality_matches_domain_fact u#b
/\ values_contains_fact u#b
/\ items_contains_fact u#b
/\ empty_domain_empty_fact u#b
/\ glue_domain_fact u#b
/\ glue_elements_fact u#b
/\ insert_elements_fact u#b
/\ insert_member_cardinality_fact u#b
/\ insert_nonmember_cardinality_fact u#b
/\ merge_domain_is_union_fact u#b
/\ merge_element_fact u#b
/\ subtract_domain_fact u#b
/\ subtract_element_fact u#b
/\ map_equal_fact u#b
/\ map_extensionality_fact u#b
/\ disjoint_fact u#b | {
"file_name": "ulib/FStar.FiniteMap.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 22,
"end_line": 460,
"start_col": 0,
"start_line": 440
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite maps as they're modeled in Dafny.
@summary Type and functions for modeling finite maps
*)
module FStar.FiniteMap.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
module FSet = FStar.FiniteSet.Base
type setfun_t (a: eqtype)
(b: Type u#b)
(s: FSet.set a) =
f: (a ^-> option b){forall (key: a). FSet.mem key s == Some? (f key)}
val map (a: eqtype) ([@@@ strictly_positive] b: Type u#b)
: Type u#b
(**
We translate each Dafny sequence function prefixed with `Map#`
into an F* function.
**)
/// We represent the Dafny function `Map#Domain` with `domain`:
///
/// function Map#Domain<U,V>(Map U V) : Set U;
val domain (#a: eqtype) (#b: Type u#b) (m: map a b)
: FSet.set a
/// We represent the Dafny function `Map#Elements` with `elements`:
///
/// function Map#Elements<U,V>(Map U V) : [U]V;
val elements (#a: eqtype) (#b: Type u#b) (m: map a b)
: setfun_t a b (domain m)
/// We represent the Dafny operator `in` on maps with `mem`:
let mem (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b) =
FSet.mem key (domain m)
/// We can convert a map to a list of pairs with `map_as_list`:
let rec key_in_item_list (#a: eqtype) (#b: Type u#b) (key: a) (items: list (a * b)) : bool =
match items with
| [] -> false
| (k, v) :: tl -> key = k || key_in_item_list key tl
let rec item_list_doesnt_repeat_keys (#a: eqtype) (#b: Type u#b) (items: list (a * b)) : bool =
match items with
| [] -> true
| (k, v) :: tl -> not (key_in_item_list k tl) && item_list_doesnt_repeat_keys tl
val map_as_list (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot (items: list (a * b){item_list_doesnt_repeat_keys items /\ (forall key. key_in_item_list key items <==> mem key m)})
/// We represent the Dafny operator [] on maps with `lookup`:
let lookup (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b{mem key m})
: b =
Some?.v ((elements m) key)
/// We represent the Dafny function `Map#Card` with `cardinality`:
///
/// function Map#Card<U,V>(Map U V) : int;
val cardinality (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot nat
/// We represent the Dafny function `Map#Values` with `values`:
///
/// function Map#Values<U,V>(Map U V) : Set V;
val values (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot (b -> prop)
/// We represent the Dafny function `Map#Items` with `items`:
///
/// function Map#Items<U,V>(Map U V) : Set Box;
val items (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot ((a * b) -> prop)
/// We represent the Dafny function `Map#Empty` with `emptymap`:
///
/// function Map#Empty<U, V>(): Map U V;
val emptymap (#a: eqtype) (#b: Type u#b)
: map a b
/// We represent the Dafny function `Map#Glue` with `glue`.
///
/// function Map#Glue<U, V>([U]bool, [U]V, Ty): Map U V;
val glue (#a: eqtype) (#b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys)
: map a b
/// We represent the Dafny function `Map#Build` with `insert`:
///
/// function Map#Build<U, V>(Map U V, U, V): Map U V;
val insert (#a: eqtype) (#b: Type u#b) (k: a) (v: b) (m: map a b)
: map a b
/// We represent the Dafny function `Map#Merge` with `merge`:
///
/// function Map#Merge<U, V>(Map U V, Map U V): Map U V;
val merge (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: map a b
/// We represent the Dafny function `Map#Subtract` with `subtract`:
///
/// function Map#Subtract<U, V>(Map U V, Set U): Map U V;
val subtract (#a: eqtype) (#b: Type u#b) (m: map a b) (s: FSet.set a)
: map a b
/// We represent the Dafny function `Map#Equal` with `equal`:
///
/// function Map#Equal<U, V>(Map U V, Map U V): bool;
val equal (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: prop
/// We represent the Dafny function `Map#Disjoint` with `disjoint`:
///
/// function Map#Disjoint<U, V>(Map U V, Map U V): bool;
val disjoint (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: prop
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (#b: Type u#b) (m: map a b{exists key. mem key m})
: GTot (key: a{mem key m})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b)
: map a b =
subtract m (FSet.singleton key)
let notin (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b)
: bool =
not (mem key m)
(**
We translate each finite map axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<U,V> m: Map U V :: { Map#Card(m) } 0 <= Map#Card(m));
/// We represent the following Dafny axiom with `cardinality_zero_iff_empty_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Card(m) }
/// Map#Card(m) == 0 <==> m == Map#Empty());
let cardinality_zero_iff_empty_fact =
forall (a: eqtype) (b:Type u#b) (m: map a b).{:pattern cardinality m}
cardinality m = 0 <==> m == emptymap
/// We represent the following Dafny axiom with `empty_or_domain_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Domain(m) }
/// m == Map#Empty() || (exists k: U :: Map#Domain(m)[k]));
let empty_or_domain_occupied_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern domain m}
m == emptymap \/ (exists k.{:pattern mem k m} mem k m)
/// We represent the following Dafny axiom with `empty_or_values_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Values(m) }
/// m == Map#Empty() || (exists v: V :: Map#Values(m)[v]));
let empty_or_values_occupied_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern values m}
m == emptymap \/ (exists v. {:pattern values m v } (values m) v)
/// We represent the following Dafny axiom with `empty_or_items_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Items(m) }
/// m == Map#Empty() || (exists k, v: Box :: Map#Items(m)[$Box(#_System._tuple#2._#Make2(k, v))]));
let empty_or_items_occupied_fact =
forall (a: eqtype) (b:Type u#b) (m: map a b).{:pattern items m}
m == emptymap \/ (exists item. {:pattern items m item } (items m) item)
/// We represent the following Dafny axiom with `map_cardinality_matches_domain_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Domain(m)) }
/// Set#Card(Map#Domain(m)) == Map#Card(m));
let map_cardinality_matches_domain_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern FSet.cardinality (domain m)}
FSet.cardinality (domain m) = cardinality m
/// We don't use the following Dafny axioms, which would require
/// treating the values and items as finite sets, which we can't do
/// because we want to allow non-eqtypes as values.
///
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Values(m)) }
/// Set#Card(Map#Values(m)) <= Map#Card(m));
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Items(m)) }
/// Set#Card(Map#Items(m)) == Map#Card(m));
/// We represent the following Dafny axiom with `values_contains_fact`:
///
/// axiom (forall<U,V> m: Map U V, v: V :: { Map#Values(m)[v] }
/// Map#Values(m)[v] ==
/// (exists u: U :: { Map#Domain(m)[u] } { Map#Elements(m)[u] }
/// Map#Domain(m)[u] &&
/// v == Map#Elements(m)[u]));
let values_contains_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (v: b).{:pattern (values m) v}
(values m) v <==>
(exists (u: a).{:pattern FSet.mem u (domain m) \/ ((elements m) u)}
FSet.mem u (domain m) /\ (elements m) u == Some v)
/// We represent the following Dafny axiom with `items_contains_fact`:
///
/// axiom (forall m: Map Box Box, item: Box :: { Map#Items(m)[item] }
/// Map#Items(m)[item] <==>
/// Map#Domain(m)[_System.Tuple2._0($Unbox(item))] &&
/// Map#Elements(m)[_System.Tuple2._0($Unbox(item))] == _System.Tuple2._1($Unbox(item)));
let items_contains_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (item: a * b).{:pattern (items m) item}
(items m) item <==>
FSet.mem (fst item) (domain m)
/\ (elements m) (fst item) == Some (snd item)
/// We represent the following Dafny axiom with `empty_domain_empty_fact`:
///
/// axiom (forall<U, V> u: U ::
/// { Map#Domain(Map#Empty(): Map U V)[u] }
/// !Map#Domain(Map#Empty(): Map U V)[u]);
let empty_domain_empty_fact =
forall (a: eqtype) (b: Type u#b) (u: a).{:pattern FSet.mem u (domain (emptymap #a #b))}
not (FSet.mem u (domain (emptymap #a #b)))
/// We represent the following Dafny axiom with `glue_domain_fact`:
///
/// axiom (forall<U, V> a: [U]bool, b: [U]V, t: Ty ::
/// { Map#Domain(Map#Glue(a, b, t)) }
/// Map#Domain(Map#Glue(a, b, t)) == a);
let glue_domain_fact =
forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).{:pattern domain (glue keys f)}
domain (glue keys f) == keys
/// We represent the following Dafny axiom with `glue_elements_fact`.
/// But we have to change it because our version of `Map#Elements`
/// returns a map to an optional value.
///
/// axiom (forall<U, V> a: [U]bool, b: [U]V, t: Ty ::
/// { Map#Elements(Map#Glue(a, b, t)) }
/// Map#Elements(Map#Glue(a, b, t)) == b);
let glue_elements_fact =
forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).{:pattern elements (glue keys f)}
domain (glue keys f) == keys
/\ elements (glue keys f) == f
/// We don't need the following Dafny axiom since the type of `glue` implies it:
///
/// axiom (forall a: [Box]bool, b: [Box]Box, t0, t1: Ty ::
/// { Map#Glue(a, b, TMap(t0, t1)) }
/// // In the following line, no trigger needed, since the quantifier only gets used in negative contexts
/// (forall bx: Box :: a[bx] ==> $IsBox(bx, t0) && $IsBox(b[bx], t1))
/// ==>
/// $Is(Map#Glue(a, b, TMap(t0, t1)), TMap(t0, t1)));
/// We represent the following Dafny axiom with `insert_elements_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, u': U, v: V ::
/// { Map#Domain(Map#Build(m, u, v))[u'] } { Map#Elements(Map#Build(m, u, v))[u'] }
/// (u' == u ==> Map#Domain(Map#Build(m, u, v))[u'] &&
/// Map#Elements(Map#Build(m, u, v))[u'] == v) &&
/// (u' != u ==> Map#Domain(Map#Build(m, u, v))[u'] == Map#Domain(m)[u'] &&
/// Map#Elements(Map#Build(m, u, v))[u'] == Map#Elements(m)[u']));
let insert_elements_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (key': a) (value: b).
{:pattern FSet.mem key' (domain (insert key value m)) \/ ((elements (insert key value m)) key')}
(key' = key ==> FSet.mem key' (domain (insert key value m))
/\ (elements (insert key value m)) key' == Some value)
/\ (key' <> key ==> FSet.mem key' (domain (insert key value m)) = FSet.mem key' (domain m)
/\ (elements (insert key value m)) key' == (elements m) key')
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, v: V :: { Map#Card(Map#Build(m, u, v)) }
/// Map#Domain(m)[u] ==> Map#Card(Map#Build(m, u, v)) == Map#Card(m));
let insert_member_cardinality_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (value: b).{:pattern cardinality (insert key value m)}
FSet.mem key (domain m) ==> cardinality (insert key value m) = cardinality m
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, v: V :: { Map#Card(Map#Build(m, u, v)) }
/// !Map#Domain(m)[u] ==> Map#Card(Map#Build(m, u, v)) == Map#Card(m) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (value: b).{:pattern cardinality (insert key value m)}
not (FSet.mem key (domain m)) ==> cardinality (insert key value m) = cardinality m + 1
/// We represent the following Dafny axiom with `merge_domain_is_union_fact`:
///
/// axiom (forall<U, V> m: Map U V, n: Map U V ::
/// { Map#Domain(Map#Merge(m, n)) }
/// Map#Domain(Map#Merge(m, n)) == Set#Union(Map#Domain(m), Map#Domain(n)));
let merge_domain_is_union_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern domain (merge m1 m2)}
domain (merge m1 m2) == FSet.union (domain m1) (domain m2)
/// We represent the following Dafny axiom with `merge_element_fact`:
///
/// axiom (forall<U, V> m: Map U V, n: Map U V, u: U ::
/// { Map#Elements(Map#Merge(m, n))[u] }
/// Map#Domain(Map#Merge(m, n))[u] ==>
/// (!Map#Domain(n)[u] ==> Map#Elements(Map#Merge(m, n))[u] == Map#Elements(m)[u]) &&
/// (Map#Domain(n)[u] ==> Map#Elements(Map#Merge(m, n))[u] == Map#Elements(n)[u]));
let merge_element_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b) (key: a).{:pattern (elements (merge m1 m2)) key}
FSet.mem key (domain (merge m1 m2)) ==>
(not (FSet.mem key (domain m2)) ==> FSet.mem key (domain m1) /\ (elements (merge m1 m2)) key == (elements m1) key)
/\ (FSet.mem key (domain m2) ==> (elements (merge m1 m2)) key == (elements m2) key)
/// We represent the following Dafny axiom with `subtract_domain_fact`:
///
/// axiom (forall<U, V> m: Map U V, s: Set U ::
/// { Map#Domain(Map#Subtract(m, s)) }
/// Map#Domain(Map#Subtract(m, s)) == Set#Difference(Map#Domain(m), s));
let subtract_domain_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (s: FSet.set a).{:pattern domain (subtract m s)}
domain (subtract m s) == FSet.difference (domain m) s
/// We represent the following Dafny axiom with `subtract_element_fact`:
///
/// axiom (forall<U, V> m: Map U V, s: Set U, u: U ::
/// { Map#Elements(Map#Subtract(m, s))[u] }
/// Map#Domain(Map#Subtract(m, s))[u] ==>
/// Map#Elements(Map#Subtract(m, s))[u] == Map#Elements(m)[u]);
let subtract_element_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (s: FSet.set a) (key: a).{:pattern (elements (subtract m s)) key}
FSet.mem key (domain (subtract m s)) ==> FSet.mem key (domain m) /\ (elements (subtract m s)) key == (elements m) key
/// We represent the following Dafny axiom with `map_equal_fact`:
///
/// axiom (forall<U, V> m: Map U V, m': Map U V::
/// { Map#Equal(m, m') }
/// Map#Equal(m, m') <==> (forall u : U :: Map#Domain(m)[u] == Map#Domain(m')[u]) &&
/// (forall u : U :: Map#Domain(m)[u] ==> Map#Elements(m)[u] == Map#Elements(m')[u]));
let map_equal_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern equal m1 m2}
equal m1 m2 <==> (forall key. FSet.mem key (domain m1) = FSet.mem key (domain m2))
/\ (forall key. FSet.mem key (domain m1) ==> (elements m1) key == (elements m2) key)
/// We represent the following Dafny axiom with `map_extensionality_fact`:
///
/// axiom (forall<U, V> m: Map U V, m': Map U V::
/// { Map#Equal(m, m') }
/// Map#Equal(m, m') ==> m == m');
let map_extensionality_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern equal m1 m2}
equal m1 m2 ==> m1 == m2
/// We represent the following Dafny axiom with `disjoint_fact`:
///
/// axiom (forall<U, V> m: Map U V, m': Map U V ::
/// { Map#Disjoint(m, m') }
/// Map#Disjoint(m, m') <==> (forall o: U :: {Map#Domain(m)[o]} {Map#Domain(m')[o]} !Map#Domain(m)[o] || !Map#Domain(m')[o]));
let disjoint_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern disjoint m1 m2}
disjoint m1 m2 <==> (forall key.{:pattern FSet.mem key (domain m1) \/ FSet.mem key (domain m2)}
not (FSet.mem key (domain m1)) || not (FSet.mem key (domain m2)))
(**
The predicate `all_finite_map_facts` collects all the Dafny finite-map axioms.
One can bring all these facts into scope with `all_finite_map_facts_lemma ()`.
**) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.FiniteSet.Base.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteMap.Base.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FiniteSet.Base",
"short_module": "FSet"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteMap",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteMap",
"short_module": null
},
{
"abbrev": 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.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_and",
"FStar.FiniteMap.Base.cardinality_zero_iff_empty_fact",
"FStar.FiniteMap.Base.empty_or_domain_occupied_fact",
"FStar.FiniteMap.Base.empty_or_values_occupied_fact",
"FStar.FiniteMap.Base.empty_or_items_occupied_fact",
"FStar.FiniteMap.Base.map_cardinality_matches_domain_fact",
"FStar.FiniteMap.Base.values_contains_fact",
"FStar.FiniteMap.Base.items_contains_fact",
"FStar.FiniteMap.Base.empty_domain_empty_fact",
"FStar.FiniteMap.Base.glue_domain_fact",
"FStar.FiniteMap.Base.glue_elements_fact",
"FStar.FiniteMap.Base.insert_elements_fact",
"FStar.FiniteMap.Base.insert_member_cardinality_fact",
"FStar.FiniteMap.Base.insert_nonmember_cardinality_fact",
"FStar.FiniteMap.Base.merge_domain_is_union_fact",
"FStar.FiniteMap.Base.merge_element_fact",
"FStar.FiniteMap.Base.subtract_domain_fact",
"FStar.FiniteMap.Base.subtract_element_fact",
"FStar.FiniteMap.Base.map_equal_fact",
"FStar.FiniteMap.Base.map_extensionality_fact",
"FStar.FiniteMap.Base.disjoint_fact"
] | [] | false | false | false | true | true | let all_finite_map_facts =
| cardinality_zero_iff_empty_fact u#b /\ empty_or_domain_occupied_fact u#b /\
empty_or_values_occupied_fact u#b /\ empty_or_items_occupied_fact u#b /\
map_cardinality_matches_domain_fact u#b /\ values_contains_fact u#b /\ items_contains_fact u#b /\
empty_domain_empty_fact u#b /\ glue_domain_fact u#b /\ glue_elements_fact u#b /\
insert_elements_fact u#b /\ insert_member_cardinality_fact u#b /\
insert_nonmember_cardinality_fact u#b /\ merge_domain_is_union_fact u#b /\ merge_element_fact u#b /\
subtract_domain_fact u#b /\ subtract_element_fact u#b /\ map_equal_fact u#b /\
map_extensionality_fact u#b /\ disjoint_fact u#b | false |
|
STLC.Core.fst | STLC.Core.close_exp_ln | val close_exp_ln (e: stlc_exp) (v: var) (n: nat)
: Lemma (requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)] | val close_exp_ln (e: stlc_exp) (v: var) (n: nat)
: Lemma (requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)] | let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 60,
"end_line": 105,
"start_col": 0,
"start_line": 95
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: STLC.Core.stlc_exp -> v: STLC.Core.var -> n: Prims.nat
-> FStar.Pervasives.Lemma (requires STLC.Core.ln' e (n - 1))
(ensures STLC.Core.ln' (STLC.Core.close_exp' e v n) n)
[SMTPat (STLC.Core.ln' (STLC.Core.close_exp' e v n) n)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"STLC.Core.stlc_exp",
"STLC.Core.var",
"Prims.nat",
"STLC.Core.index",
"STLC.Core.stlc_ty",
"STLC.Core.close_exp_ln",
"Prims.op_Addition",
"Prims.unit",
"Prims.b2t",
"STLC.Core.ln'",
"Prims.op_Subtraction",
"Prims.squash",
"STLC.Core.close_exp'",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.bool",
"Prims.Nil"
] | [
"recursion"
] | false | false | true | false | false | let rec close_exp_ln (e: stlc_exp) (v: var) (n: nat)
: Lemma (requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)] =
| match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 ->
close_exp_ln e1 v n;
close_exp_ln e2 v n | false |
FStar.FiniteMap.Base.fsti | FStar.FiniteMap.Base.map_extensionality_fact | val map_extensionality_fact : Prims.logical | let map_extensionality_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern equal m1 m2}
equal m1 m2 ==> m1 == m2 | {
"file_name": "ulib/FStar.FiniteMap.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 28,
"end_line": 422,
"start_col": 0,
"start_line": 420
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite maps as they're modeled in Dafny.
@summary Type and functions for modeling finite maps
*)
module FStar.FiniteMap.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
module FSet = FStar.FiniteSet.Base
type setfun_t (a: eqtype)
(b: Type u#b)
(s: FSet.set a) =
f: (a ^-> option b){forall (key: a). FSet.mem key s == Some? (f key)}
val map (a: eqtype) ([@@@ strictly_positive] b: Type u#b)
: Type u#b
(**
We translate each Dafny sequence function prefixed with `Map#`
into an F* function.
**)
/// We represent the Dafny function `Map#Domain` with `domain`:
///
/// function Map#Domain<U,V>(Map U V) : Set U;
val domain (#a: eqtype) (#b: Type u#b) (m: map a b)
: FSet.set a
/// We represent the Dafny function `Map#Elements` with `elements`:
///
/// function Map#Elements<U,V>(Map U V) : [U]V;
val elements (#a: eqtype) (#b: Type u#b) (m: map a b)
: setfun_t a b (domain m)
/// We represent the Dafny operator `in` on maps with `mem`:
let mem (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b) =
FSet.mem key (domain m)
/// We can convert a map to a list of pairs with `map_as_list`:
let rec key_in_item_list (#a: eqtype) (#b: Type u#b) (key: a) (items: list (a * b)) : bool =
match items with
| [] -> false
| (k, v) :: tl -> key = k || key_in_item_list key tl
let rec item_list_doesnt_repeat_keys (#a: eqtype) (#b: Type u#b) (items: list (a * b)) : bool =
match items with
| [] -> true
| (k, v) :: tl -> not (key_in_item_list k tl) && item_list_doesnt_repeat_keys tl
val map_as_list (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot (items: list (a * b){item_list_doesnt_repeat_keys items /\ (forall key. key_in_item_list key items <==> mem key m)})
/// We represent the Dafny operator [] on maps with `lookup`:
let lookup (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b{mem key m})
: b =
Some?.v ((elements m) key)
/// We represent the Dafny function `Map#Card` with `cardinality`:
///
/// function Map#Card<U,V>(Map U V) : int;
val cardinality (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot nat
/// We represent the Dafny function `Map#Values` with `values`:
///
/// function Map#Values<U,V>(Map U V) : Set V;
val values (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot (b -> prop)
/// We represent the Dafny function `Map#Items` with `items`:
///
/// function Map#Items<U,V>(Map U V) : Set Box;
val items (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot ((a * b) -> prop)
/// We represent the Dafny function `Map#Empty` with `emptymap`:
///
/// function Map#Empty<U, V>(): Map U V;
val emptymap (#a: eqtype) (#b: Type u#b)
: map a b
/// We represent the Dafny function `Map#Glue` with `glue`.
///
/// function Map#Glue<U, V>([U]bool, [U]V, Ty): Map U V;
val glue (#a: eqtype) (#b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys)
: map a b
/// We represent the Dafny function `Map#Build` with `insert`:
///
/// function Map#Build<U, V>(Map U V, U, V): Map U V;
val insert (#a: eqtype) (#b: Type u#b) (k: a) (v: b) (m: map a b)
: map a b
/// We represent the Dafny function `Map#Merge` with `merge`:
///
/// function Map#Merge<U, V>(Map U V, Map U V): Map U V;
val merge (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: map a b
/// We represent the Dafny function `Map#Subtract` with `subtract`:
///
/// function Map#Subtract<U, V>(Map U V, Set U): Map U V;
val subtract (#a: eqtype) (#b: Type u#b) (m: map a b) (s: FSet.set a)
: map a b
/// We represent the Dafny function `Map#Equal` with `equal`:
///
/// function Map#Equal<U, V>(Map U V, Map U V): bool;
val equal (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: prop
/// We represent the Dafny function `Map#Disjoint` with `disjoint`:
///
/// function Map#Disjoint<U, V>(Map U V, Map U V): bool;
val disjoint (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: prop
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (#b: Type u#b) (m: map a b{exists key. mem key m})
: GTot (key: a{mem key m})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b)
: map a b =
subtract m (FSet.singleton key)
let notin (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b)
: bool =
not (mem key m)
(**
We translate each finite map axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<U,V> m: Map U V :: { Map#Card(m) } 0 <= Map#Card(m));
/// We represent the following Dafny axiom with `cardinality_zero_iff_empty_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Card(m) }
/// Map#Card(m) == 0 <==> m == Map#Empty());
let cardinality_zero_iff_empty_fact =
forall (a: eqtype) (b:Type u#b) (m: map a b).{:pattern cardinality m}
cardinality m = 0 <==> m == emptymap
/// We represent the following Dafny axiom with `empty_or_domain_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Domain(m) }
/// m == Map#Empty() || (exists k: U :: Map#Domain(m)[k]));
let empty_or_domain_occupied_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern domain m}
m == emptymap \/ (exists k.{:pattern mem k m} mem k m)
/// We represent the following Dafny axiom with `empty_or_values_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Values(m) }
/// m == Map#Empty() || (exists v: V :: Map#Values(m)[v]));
let empty_or_values_occupied_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern values m}
m == emptymap \/ (exists v. {:pattern values m v } (values m) v)
/// We represent the following Dafny axiom with `empty_or_items_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Items(m) }
/// m == Map#Empty() || (exists k, v: Box :: Map#Items(m)[$Box(#_System._tuple#2._#Make2(k, v))]));
let empty_or_items_occupied_fact =
forall (a: eqtype) (b:Type u#b) (m: map a b).{:pattern items m}
m == emptymap \/ (exists item. {:pattern items m item } (items m) item)
/// We represent the following Dafny axiom with `map_cardinality_matches_domain_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Domain(m)) }
/// Set#Card(Map#Domain(m)) == Map#Card(m));
let map_cardinality_matches_domain_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern FSet.cardinality (domain m)}
FSet.cardinality (domain m) = cardinality m
/// We don't use the following Dafny axioms, which would require
/// treating the values and items as finite sets, which we can't do
/// because we want to allow non-eqtypes as values.
///
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Values(m)) }
/// Set#Card(Map#Values(m)) <= Map#Card(m));
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Items(m)) }
/// Set#Card(Map#Items(m)) == Map#Card(m));
/// We represent the following Dafny axiom with `values_contains_fact`:
///
/// axiom (forall<U,V> m: Map U V, v: V :: { Map#Values(m)[v] }
/// Map#Values(m)[v] ==
/// (exists u: U :: { Map#Domain(m)[u] } { Map#Elements(m)[u] }
/// Map#Domain(m)[u] &&
/// v == Map#Elements(m)[u]));
let values_contains_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (v: b).{:pattern (values m) v}
(values m) v <==>
(exists (u: a).{:pattern FSet.mem u (domain m) \/ ((elements m) u)}
FSet.mem u (domain m) /\ (elements m) u == Some v)
/// We represent the following Dafny axiom with `items_contains_fact`:
///
/// axiom (forall m: Map Box Box, item: Box :: { Map#Items(m)[item] }
/// Map#Items(m)[item] <==>
/// Map#Domain(m)[_System.Tuple2._0($Unbox(item))] &&
/// Map#Elements(m)[_System.Tuple2._0($Unbox(item))] == _System.Tuple2._1($Unbox(item)));
let items_contains_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (item: a * b).{:pattern (items m) item}
(items m) item <==>
FSet.mem (fst item) (domain m)
/\ (elements m) (fst item) == Some (snd item)
/// We represent the following Dafny axiom with `empty_domain_empty_fact`:
///
/// axiom (forall<U, V> u: U ::
/// { Map#Domain(Map#Empty(): Map U V)[u] }
/// !Map#Domain(Map#Empty(): Map U V)[u]);
let empty_domain_empty_fact =
forall (a: eqtype) (b: Type u#b) (u: a).{:pattern FSet.mem u (domain (emptymap #a #b))}
not (FSet.mem u (domain (emptymap #a #b)))
/// We represent the following Dafny axiom with `glue_domain_fact`:
///
/// axiom (forall<U, V> a: [U]bool, b: [U]V, t: Ty ::
/// { Map#Domain(Map#Glue(a, b, t)) }
/// Map#Domain(Map#Glue(a, b, t)) == a);
let glue_domain_fact =
forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).{:pattern domain (glue keys f)}
domain (glue keys f) == keys
/// We represent the following Dafny axiom with `glue_elements_fact`.
/// But we have to change it because our version of `Map#Elements`
/// returns a map to an optional value.
///
/// axiom (forall<U, V> a: [U]bool, b: [U]V, t: Ty ::
/// { Map#Elements(Map#Glue(a, b, t)) }
/// Map#Elements(Map#Glue(a, b, t)) == b);
let glue_elements_fact =
forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).{:pattern elements (glue keys f)}
domain (glue keys f) == keys
/\ elements (glue keys f) == f
/// We don't need the following Dafny axiom since the type of `glue` implies it:
///
/// axiom (forall a: [Box]bool, b: [Box]Box, t0, t1: Ty ::
/// { Map#Glue(a, b, TMap(t0, t1)) }
/// // In the following line, no trigger needed, since the quantifier only gets used in negative contexts
/// (forall bx: Box :: a[bx] ==> $IsBox(bx, t0) && $IsBox(b[bx], t1))
/// ==>
/// $Is(Map#Glue(a, b, TMap(t0, t1)), TMap(t0, t1)));
/// We represent the following Dafny axiom with `insert_elements_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, u': U, v: V ::
/// { Map#Domain(Map#Build(m, u, v))[u'] } { Map#Elements(Map#Build(m, u, v))[u'] }
/// (u' == u ==> Map#Domain(Map#Build(m, u, v))[u'] &&
/// Map#Elements(Map#Build(m, u, v))[u'] == v) &&
/// (u' != u ==> Map#Domain(Map#Build(m, u, v))[u'] == Map#Domain(m)[u'] &&
/// Map#Elements(Map#Build(m, u, v))[u'] == Map#Elements(m)[u']));
let insert_elements_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (key': a) (value: b).
{:pattern FSet.mem key' (domain (insert key value m)) \/ ((elements (insert key value m)) key')}
(key' = key ==> FSet.mem key' (domain (insert key value m))
/\ (elements (insert key value m)) key' == Some value)
/\ (key' <> key ==> FSet.mem key' (domain (insert key value m)) = FSet.mem key' (domain m)
/\ (elements (insert key value m)) key' == (elements m) key')
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, v: V :: { Map#Card(Map#Build(m, u, v)) }
/// Map#Domain(m)[u] ==> Map#Card(Map#Build(m, u, v)) == Map#Card(m));
let insert_member_cardinality_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (value: b).{:pattern cardinality (insert key value m)}
FSet.mem key (domain m) ==> cardinality (insert key value m) = cardinality m
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, v: V :: { Map#Card(Map#Build(m, u, v)) }
/// !Map#Domain(m)[u] ==> Map#Card(Map#Build(m, u, v)) == Map#Card(m) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (value: b).{:pattern cardinality (insert key value m)}
not (FSet.mem key (domain m)) ==> cardinality (insert key value m) = cardinality m + 1
/// We represent the following Dafny axiom with `merge_domain_is_union_fact`:
///
/// axiom (forall<U, V> m: Map U V, n: Map U V ::
/// { Map#Domain(Map#Merge(m, n)) }
/// Map#Domain(Map#Merge(m, n)) == Set#Union(Map#Domain(m), Map#Domain(n)));
let merge_domain_is_union_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern domain (merge m1 m2)}
domain (merge m1 m2) == FSet.union (domain m1) (domain m2)
/// We represent the following Dafny axiom with `merge_element_fact`:
///
/// axiom (forall<U, V> m: Map U V, n: Map U V, u: U ::
/// { Map#Elements(Map#Merge(m, n))[u] }
/// Map#Domain(Map#Merge(m, n))[u] ==>
/// (!Map#Domain(n)[u] ==> Map#Elements(Map#Merge(m, n))[u] == Map#Elements(m)[u]) &&
/// (Map#Domain(n)[u] ==> Map#Elements(Map#Merge(m, n))[u] == Map#Elements(n)[u]));
let merge_element_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b) (key: a).{:pattern (elements (merge m1 m2)) key}
FSet.mem key (domain (merge m1 m2)) ==>
(not (FSet.mem key (domain m2)) ==> FSet.mem key (domain m1) /\ (elements (merge m1 m2)) key == (elements m1) key)
/\ (FSet.mem key (domain m2) ==> (elements (merge m1 m2)) key == (elements m2) key)
/// We represent the following Dafny axiom with `subtract_domain_fact`:
///
/// axiom (forall<U, V> m: Map U V, s: Set U ::
/// { Map#Domain(Map#Subtract(m, s)) }
/// Map#Domain(Map#Subtract(m, s)) == Set#Difference(Map#Domain(m), s));
let subtract_domain_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (s: FSet.set a).{:pattern domain (subtract m s)}
domain (subtract m s) == FSet.difference (domain m) s
/// We represent the following Dafny axiom with `subtract_element_fact`:
///
/// axiom (forall<U, V> m: Map U V, s: Set U, u: U ::
/// { Map#Elements(Map#Subtract(m, s))[u] }
/// Map#Domain(Map#Subtract(m, s))[u] ==>
/// Map#Elements(Map#Subtract(m, s))[u] == Map#Elements(m)[u]);
let subtract_element_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (s: FSet.set a) (key: a).{:pattern (elements (subtract m s)) key}
FSet.mem key (domain (subtract m s)) ==> FSet.mem key (domain m) /\ (elements (subtract m s)) key == (elements m) key
/// We represent the following Dafny axiom with `map_equal_fact`:
///
/// axiom (forall<U, V> m: Map U V, m': Map U V::
/// { Map#Equal(m, m') }
/// Map#Equal(m, m') <==> (forall u : U :: Map#Domain(m)[u] == Map#Domain(m')[u]) &&
/// (forall u : U :: Map#Domain(m)[u] ==> Map#Elements(m)[u] == Map#Elements(m')[u]));
let map_equal_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern equal m1 m2}
equal m1 m2 <==> (forall key. FSet.mem key (domain m1) = FSet.mem key (domain m2))
/\ (forall key. FSet.mem key (domain m1) ==> (elements m1) key == (elements m2) key)
/// We represent the following Dafny axiom with `map_extensionality_fact`:
///
/// axiom (forall<U, V> m: Map U V, m': Map U V::
/// { Map#Equal(m, m') }
/// Map#Equal(m, m') ==> m == m'); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.FiniteSet.Base.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteMap.Base.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FiniteSet.Base",
"short_module": "FSet"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteMap",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteMap",
"short_module": null
},
{
"abbrev": 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.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteMap.Base.map",
"Prims.l_imp",
"FStar.FiniteMap.Base.equal",
"Prims.eq2"
] | [] | false | false | false | true | true | let map_extensionality_fact =
| forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b). {:pattern equal m1 m2}
equal m1 m2 ==> m1 == m2 | false |
|
FStar.FiniteMap.Base.fsti | FStar.FiniteMap.Base.subtract_domain_fact | val subtract_domain_fact : Prims.logical | let subtract_domain_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (s: FSet.set a).{:pattern domain (subtract m s)}
domain (subtract m s) == FSet.difference (domain m) s | {
"file_name": "ulib/FStar.FiniteMap.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 57,
"end_line": 389,
"start_col": 0,
"start_line": 387
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite maps as they're modeled in Dafny.
@summary Type and functions for modeling finite maps
*)
module FStar.FiniteMap.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
module FSet = FStar.FiniteSet.Base
type setfun_t (a: eqtype)
(b: Type u#b)
(s: FSet.set a) =
f: (a ^-> option b){forall (key: a). FSet.mem key s == Some? (f key)}
val map (a: eqtype) ([@@@ strictly_positive] b: Type u#b)
: Type u#b
(**
We translate each Dafny sequence function prefixed with `Map#`
into an F* function.
**)
/// We represent the Dafny function `Map#Domain` with `domain`:
///
/// function Map#Domain<U,V>(Map U V) : Set U;
val domain (#a: eqtype) (#b: Type u#b) (m: map a b)
: FSet.set a
/// We represent the Dafny function `Map#Elements` with `elements`:
///
/// function Map#Elements<U,V>(Map U V) : [U]V;
val elements (#a: eqtype) (#b: Type u#b) (m: map a b)
: setfun_t a b (domain m)
/// We represent the Dafny operator `in` on maps with `mem`:
let mem (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b) =
FSet.mem key (domain m)
/// We can convert a map to a list of pairs with `map_as_list`:
let rec key_in_item_list (#a: eqtype) (#b: Type u#b) (key: a) (items: list (a * b)) : bool =
match items with
| [] -> false
| (k, v) :: tl -> key = k || key_in_item_list key tl
let rec item_list_doesnt_repeat_keys (#a: eqtype) (#b: Type u#b) (items: list (a * b)) : bool =
match items with
| [] -> true
| (k, v) :: tl -> not (key_in_item_list k tl) && item_list_doesnt_repeat_keys tl
val map_as_list (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot (items: list (a * b){item_list_doesnt_repeat_keys items /\ (forall key. key_in_item_list key items <==> mem key m)})
/// We represent the Dafny operator [] on maps with `lookup`:
let lookup (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b{mem key m})
: b =
Some?.v ((elements m) key)
/// We represent the Dafny function `Map#Card` with `cardinality`:
///
/// function Map#Card<U,V>(Map U V) : int;
val cardinality (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot nat
/// We represent the Dafny function `Map#Values` with `values`:
///
/// function Map#Values<U,V>(Map U V) : Set V;
val values (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot (b -> prop)
/// We represent the Dafny function `Map#Items` with `items`:
///
/// function Map#Items<U,V>(Map U V) : Set Box;
val items (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot ((a * b) -> prop)
/// We represent the Dafny function `Map#Empty` with `emptymap`:
///
/// function Map#Empty<U, V>(): Map U V;
val emptymap (#a: eqtype) (#b: Type u#b)
: map a b
/// We represent the Dafny function `Map#Glue` with `glue`.
///
/// function Map#Glue<U, V>([U]bool, [U]V, Ty): Map U V;
val glue (#a: eqtype) (#b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys)
: map a b
/// We represent the Dafny function `Map#Build` with `insert`:
///
/// function Map#Build<U, V>(Map U V, U, V): Map U V;
val insert (#a: eqtype) (#b: Type u#b) (k: a) (v: b) (m: map a b)
: map a b
/// We represent the Dafny function `Map#Merge` with `merge`:
///
/// function Map#Merge<U, V>(Map U V, Map U V): Map U V;
val merge (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: map a b
/// We represent the Dafny function `Map#Subtract` with `subtract`:
///
/// function Map#Subtract<U, V>(Map U V, Set U): Map U V;
val subtract (#a: eqtype) (#b: Type u#b) (m: map a b) (s: FSet.set a)
: map a b
/// We represent the Dafny function `Map#Equal` with `equal`:
///
/// function Map#Equal<U, V>(Map U V, Map U V): bool;
val equal (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: prop
/// We represent the Dafny function `Map#Disjoint` with `disjoint`:
///
/// function Map#Disjoint<U, V>(Map U V, Map U V): bool;
val disjoint (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: prop
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (#b: Type u#b) (m: map a b{exists key. mem key m})
: GTot (key: a{mem key m})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b)
: map a b =
subtract m (FSet.singleton key)
let notin (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b)
: bool =
not (mem key m)
(**
We translate each finite map axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<U,V> m: Map U V :: { Map#Card(m) } 0 <= Map#Card(m));
/// We represent the following Dafny axiom with `cardinality_zero_iff_empty_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Card(m) }
/// Map#Card(m) == 0 <==> m == Map#Empty());
let cardinality_zero_iff_empty_fact =
forall (a: eqtype) (b:Type u#b) (m: map a b).{:pattern cardinality m}
cardinality m = 0 <==> m == emptymap
/// We represent the following Dafny axiom with `empty_or_domain_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Domain(m) }
/// m == Map#Empty() || (exists k: U :: Map#Domain(m)[k]));
let empty_or_domain_occupied_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern domain m}
m == emptymap \/ (exists k.{:pattern mem k m} mem k m)
/// We represent the following Dafny axiom with `empty_or_values_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Values(m) }
/// m == Map#Empty() || (exists v: V :: Map#Values(m)[v]));
let empty_or_values_occupied_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern values m}
m == emptymap \/ (exists v. {:pattern values m v } (values m) v)
/// We represent the following Dafny axiom with `empty_or_items_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Items(m) }
/// m == Map#Empty() || (exists k, v: Box :: Map#Items(m)[$Box(#_System._tuple#2._#Make2(k, v))]));
let empty_or_items_occupied_fact =
forall (a: eqtype) (b:Type u#b) (m: map a b).{:pattern items m}
m == emptymap \/ (exists item. {:pattern items m item } (items m) item)
/// We represent the following Dafny axiom with `map_cardinality_matches_domain_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Domain(m)) }
/// Set#Card(Map#Domain(m)) == Map#Card(m));
let map_cardinality_matches_domain_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern FSet.cardinality (domain m)}
FSet.cardinality (domain m) = cardinality m
/// We don't use the following Dafny axioms, which would require
/// treating the values and items as finite sets, which we can't do
/// because we want to allow non-eqtypes as values.
///
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Values(m)) }
/// Set#Card(Map#Values(m)) <= Map#Card(m));
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Items(m)) }
/// Set#Card(Map#Items(m)) == Map#Card(m));
/// We represent the following Dafny axiom with `values_contains_fact`:
///
/// axiom (forall<U,V> m: Map U V, v: V :: { Map#Values(m)[v] }
/// Map#Values(m)[v] ==
/// (exists u: U :: { Map#Domain(m)[u] } { Map#Elements(m)[u] }
/// Map#Domain(m)[u] &&
/// v == Map#Elements(m)[u]));
let values_contains_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (v: b).{:pattern (values m) v}
(values m) v <==>
(exists (u: a).{:pattern FSet.mem u (domain m) \/ ((elements m) u)}
FSet.mem u (domain m) /\ (elements m) u == Some v)
/// We represent the following Dafny axiom with `items_contains_fact`:
///
/// axiom (forall m: Map Box Box, item: Box :: { Map#Items(m)[item] }
/// Map#Items(m)[item] <==>
/// Map#Domain(m)[_System.Tuple2._0($Unbox(item))] &&
/// Map#Elements(m)[_System.Tuple2._0($Unbox(item))] == _System.Tuple2._1($Unbox(item)));
let items_contains_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (item: a * b).{:pattern (items m) item}
(items m) item <==>
FSet.mem (fst item) (domain m)
/\ (elements m) (fst item) == Some (snd item)
/// We represent the following Dafny axiom with `empty_domain_empty_fact`:
///
/// axiom (forall<U, V> u: U ::
/// { Map#Domain(Map#Empty(): Map U V)[u] }
/// !Map#Domain(Map#Empty(): Map U V)[u]);
let empty_domain_empty_fact =
forall (a: eqtype) (b: Type u#b) (u: a).{:pattern FSet.mem u (domain (emptymap #a #b))}
not (FSet.mem u (domain (emptymap #a #b)))
/// We represent the following Dafny axiom with `glue_domain_fact`:
///
/// axiom (forall<U, V> a: [U]bool, b: [U]V, t: Ty ::
/// { Map#Domain(Map#Glue(a, b, t)) }
/// Map#Domain(Map#Glue(a, b, t)) == a);
let glue_domain_fact =
forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).{:pattern domain (glue keys f)}
domain (glue keys f) == keys
/// We represent the following Dafny axiom with `glue_elements_fact`.
/// But we have to change it because our version of `Map#Elements`
/// returns a map to an optional value.
///
/// axiom (forall<U, V> a: [U]bool, b: [U]V, t: Ty ::
/// { Map#Elements(Map#Glue(a, b, t)) }
/// Map#Elements(Map#Glue(a, b, t)) == b);
let glue_elements_fact =
forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).{:pattern elements (glue keys f)}
domain (glue keys f) == keys
/\ elements (glue keys f) == f
/// We don't need the following Dafny axiom since the type of `glue` implies it:
///
/// axiom (forall a: [Box]bool, b: [Box]Box, t0, t1: Ty ::
/// { Map#Glue(a, b, TMap(t0, t1)) }
/// // In the following line, no trigger needed, since the quantifier only gets used in negative contexts
/// (forall bx: Box :: a[bx] ==> $IsBox(bx, t0) && $IsBox(b[bx], t1))
/// ==>
/// $Is(Map#Glue(a, b, TMap(t0, t1)), TMap(t0, t1)));
/// We represent the following Dafny axiom with `insert_elements_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, u': U, v: V ::
/// { Map#Domain(Map#Build(m, u, v))[u'] } { Map#Elements(Map#Build(m, u, v))[u'] }
/// (u' == u ==> Map#Domain(Map#Build(m, u, v))[u'] &&
/// Map#Elements(Map#Build(m, u, v))[u'] == v) &&
/// (u' != u ==> Map#Domain(Map#Build(m, u, v))[u'] == Map#Domain(m)[u'] &&
/// Map#Elements(Map#Build(m, u, v))[u'] == Map#Elements(m)[u']));
let insert_elements_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (key': a) (value: b).
{:pattern FSet.mem key' (domain (insert key value m)) \/ ((elements (insert key value m)) key')}
(key' = key ==> FSet.mem key' (domain (insert key value m))
/\ (elements (insert key value m)) key' == Some value)
/\ (key' <> key ==> FSet.mem key' (domain (insert key value m)) = FSet.mem key' (domain m)
/\ (elements (insert key value m)) key' == (elements m) key')
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, v: V :: { Map#Card(Map#Build(m, u, v)) }
/// Map#Domain(m)[u] ==> Map#Card(Map#Build(m, u, v)) == Map#Card(m));
let insert_member_cardinality_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (value: b).{:pattern cardinality (insert key value m)}
FSet.mem key (domain m) ==> cardinality (insert key value m) = cardinality m
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, v: V :: { Map#Card(Map#Build(m, u, v)) }
/// !Map#Domain(m)[u] ==> Map#Card(Map#Build(m, u, v)) == Map#Card(m) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (value: b).{:pattern cardinality (insert key value m)}
not (FSet.mem key (domain m)) ==> cardinality (insert key value m) = cardinality m + 1
/// We represent the following Dafny axiom with `merge_domain_is_union_fact`:
///
/// axiom (forall<U, V> m: Map U V, n: Map U V ::
/// { Map#Domain(Map#Merge(m, n)) }
/// Map#Domain(Map#Merge(m, n)) == Set#Union(Map#Domain(m), Map#Domain(n)));
let merge_domain_is_union_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern domain (merge m1 m2)}
domain (merge m1 m2) == FSet.union (domain m1) (domain m2)
/// We represent the following Dafny axiom with `merge_element_fact`:
///
/// axiom (forall<U, V> m: Map U V, n: Map U V, u: U ::
/// { Map#Elements(Map#Merge(m, n))[u] }
/// Map#Domain(Map#Merge(m, n))[u] ==>
/// (!Map#Domain(n)[u] ==> Map#Elements(Map#Merge(m, n))[u] == Map#Elements(m)[u]) &&
/// (Map#Domain(n)[u] ==> Map#Elements(Map#Merge(m, n))[u] == Map#Elements(n)[u]));
let merge_element_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b) (key: a).{:pattern (elements (merge m1 m2)) key}
FSet.mem key (domain (merge m1 m2)) ==>
(not (FSet.mem key (domain m2)) ==> FSet.mem key (domain m1) /\ (elements (merge m1 m2)) key == (elements m1) key)
/\ (FSet.mem key (domain m2) ==> (elements (merge m1 m2)) key == (elements m2) key)
/// We represent the following Dafny axiom with `subtract_domain_fact`:
///
/// axiom (forall<U, V> m: Map U V, s: Set U ::
/// { Map#Domain(Map#Subtract(m, s)) }
/// Map#Domain(Map#Subtract(m, s)) == Set#Difference(Map#Domain(m), s)); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.FiniteSet.Base.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteMap.Base.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FiniteSet.Base",
"short_module": "FSet"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteMap",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteMap",
"short_module": null
},
{
"abbrev": 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.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteMap.Base.map",
"FStar.FiniteSet.Base.set",
"Prims.eq2",
"FStar.FiniteMap.Base.domain",
"FStar.FiniteMap.Base.subtract",
"FStar.FiniteSet.Base.difference"
] | [] | false | false | false | true | true | let subtract_domain_fact =
| forall (a: eqtype) (b: Type u#b) (m: map a b) (s: FSet.set a). {:pattern domain (subtract m s)}
domain (subtract m s) == FSet.difference (domain m) s | false |
|
FStar.FiniteMap.Base.fsti | FStar.FiniteMap.Base.disjoint_fact | val disjoint_fact : Prims.logical | let disjoint_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern disjoint m1 m2}
disjoint m1 m2 <==> (forall key.{:pattern FSet.mem key (domain m1) \/ FSet.mem key (domain m2)}
not (FSet.mem key (domain m1)) || not (FSet.mem key (domain m2))) | {
"file_name": "ulib/FStar.FiniteMap.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 91,
"end_line": 433,
"start_col": 0,
"start_line": 430
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite maps as they're modeled in Dafny.
@summary Type and functions for modeling finite maps
*)
module FStar.FiniteMap.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
module FSet = FStar.FiniteSet.Base
type setfun_t (a: eqtype)
(b: Type u#b)
(s: FSet.set a) =
f: (a ^-> option b){forall (key: a). FSet.mem key s == Some? (f key)}
val map (a: eqtype) ([@@@ strictly_positive] b: Type u#b)
: Type u#b
(**
We translate each Dafny sequence function prefixed with `Map#`
into an F* function.
**)
/// We represent the Dafny function `Map#Domain` with `domain`:
///
/// function Map#Domain<U,V>(Map U V) : Set U;
val domain (#a: eqtype) (#b: Type u#b) (m: map a b)
: FSet.set a
/// We represent the Dafny function `Map#Elements` with `elements`:
///
/// function Map#Elements<U,V>(Map U V) : [U]V;
val elements (#a: eqtype) (#b: Type u#b) (m: map a b)
: setfun_t a b (domain m)
/// We represent the Dafny operator `in` on maps with `mem`:
let mem (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b) =
FSet.mem key (domain m)
/// We can convert a map to a list of pairs with `map_as_list`:
let rec key_in_item_list (#a: eqtype) (#b: Type u#b) (key: a) (items: list (a * b)) : bool =
match items with
| [] -> false
| (k, v) :: tl -> key = k || key_in_item_list key tl
let rec item_list_doesnt_repeat_keys (#a: eqtype) (#b: Type u#b) (items: list (a * b)) : bool =
match items with
| [] -> true
| (k, v) :: tl -> not (key_in_item_list k tl) && item_list_doesnt_repeat_keys tl
val map_as_list (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot (items: list (a * b){item_list_doesnt_repeat_keys items /\ (forall key. key_in_item_list key items <==> mem key m)})
/// We represent the Dafny operator [] on maps with `lookup`:
let lookup (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b{mem key m})
: b =
Some?.v ((elements m) key)
/// We represent the Dafny function `Map#Card` with `cardinality`:
///
/// function Map#Card<U,V>(Map U V) : int;
val cardinality (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot nat
/// We represent the Dafny function `Map#Values` with `values`:
///
/// function Map#Values<U,V>(Map U V) : Set V;
val values (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot (b -> prop)
/// We represent the Dafny function `Map#Items` with `items`:
///
/// function Map#Items<U,V>(Map U V) : Set Box;
val items (#a: eqtype) (#b: Type u#b) (m: map a b)
: GTot ((a * b) -> prop)
/// We represent the Dafny function `Map#Empty` with `emptymap`:
///
/// function Map#Empty<U, V>(): Map U V;
val emptymap (#a: eqtype) (#b: Type u#b)
: map a b
/// We represent the Dafny function `Map#Glue` with `glue`.
///
/// function Map#Glue<U, V>([U]bool, [U]V, Ty): Map U V;
val glue (#a: eqtype) (#b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys)
: map a b
/// We represent the Dafny function `Map#Build` with `insert`:
///
/// function Map#Build<U, V>(Map U V, U, V): Map U V;
val insert (#a: eqtype) (#b: Type u#b) (k: a) (v: b) (m: map a b)
: map a b
/// We represent the Dafny function `Map#Merge` with `merge`:
///
/// function Map#Merge<U, V>(Map U V, Map U V): Map U V;
val merge (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: map a b
/// We represent the Dafny function `Map#Subtract` with `subtract`:
///
/// function Map#Subtract<U, V>(Map U V, Set U): Map U V;
val subtract (#a: eqtype) (#b: Type u#b) (m: map a b) (s: FSet.set a)
: map a b
/// We represent the Dafny function `Map#Equal` with `equal`:
///
/// function Map#Equal<U, V>(Map U V, Map U V): bool;
val equal (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: prop
/// We represent the Dafny function `Map#Disjoint` with `disjoint`:
///
/// function Map#Disjoint<U, V>(Map U V, Map U V): bool;
val disjoint (#a: eqtype) (#b: Type u#b) (m1: map a b) (m2: map a b)
: prop
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (#b: Type u#b) (m: map a b{exists key. mem key m})
: GTot (key: a{mem key m})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b)
: map a b =
subtract m (FSet.singleton key)
let notin (#a: eqtype) (#b: Type u#b) (key: a) (m: map a b)
: bool =
not (mem key m)
(**
We translate each finite map axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<U,V> m: Map U V :: { Map#Card(m) } 0 <= Map#Card(m));
/// We represent the following Dafny axiom with `cardinality_zero_iff_empty_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Card(m) }
/// Map#Card(m) == 0 <==> m == Map#Empty());
let cardinality_zero_iff_empty_fact =
forall (a: eqtype) (b:Type u#b) (m: map a b).{:pattern cardinality m}
cardinality m = 0 <==> m == emptymap
/// We represent the following Dafny axiom with `empty_or_domain_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Domain(m) }
/// m == Map#Empty() || (exists k: U :: Map#Domain(m)[k]));
let empty_or_domain_occupied_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern domain m}
m == emptymap \/ (exists k.{:pattern mem k m} mem k m)
/// We represent the following Dafny axiom with `empty_or_values_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Values(m) }
/// m == Map#Empty() || (exists v: V :: Map#Values(m)[v]));
let empty_or_values_occupied_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern values m}
m == emptymap \/ (exists v. {:pattern values m v } (values m) v)
/// We represent the following Dafny axiom with `empty_or_items_occupied_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Map#Items(m) }
/// m == Map#Empty() || (exists k, v: Box :: Map#Items(m)[$Box(#_System._tuple#2._#Make2(k, v))]));
let empty_or_items_occupied_fact =
forall (a: eqtype) (b:Type u#b) (m: map a b).{:pattern items m}
m == emptymap \/ (exists item. {:pattern items m item } (items m) item)
/// We represent the following Dafny axiom with `map_cardinality_matches_domain_fact`:
///
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Domain(m)) }
/// Set#Card(Map#Domain(m)) == Map#Card(m));
let map_cardinality_matches_domain_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b).{:pattern FSet.cardinality (domain m)}
FSet.cardinality (domain m) = cardinality m
/// We don't use the following Dafny axioms, which would require
/// treating the values and items as finite sets, which we can't do
/// because we want to allow non-eqtypes as values.
///
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Values(m)) }
/// Set#Card(Map#Values(m)) <= Map#Card(m));
/// axiom (forall<U, V> m: Map U V ::
/// { Set#Card(Map#Items(m)) }
/// Set#Card(Map#Items(m)) == Map#Card(m));
/// We represent the following Dafny axiom with `values_contains_fact`:
///
/// axiom (forall<U,V> m: Map U V, v: V :: { Map#Values(m)[v] }
/// Map#Values(m)[v] ==
/// (exists u: U :: { Map#Domain(m)[u] } { Map#Elements(m)[u] }
/// Map#Domain(m)[u] &&
/// v == Map#Elements(m)[u]));
let values_contains_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (v: b).{:pattern (values m) v}
(values m) v <==>
(exists (u: a).{:pattern FSet.mem u (domain m) \/ ((elements m) u)}
FSet.mem u (domain m) /\ (elements m) u == Some v)
/// We represent the following Dafny axiom with `items_contains_fact`:
///
/// axiom (forall m: Map Box Box, item: Box :: { Map#Items(m)[item] }
/// Map#Items(m)[item] <==>
/// Map#Domain(m)[_System.Tuple2._0($Unbox(item))] &&
/// Map#Elements(m)[_System.Tuple2._0($Unbox(item))] == _System.Tuple2._1($Unbox(item)));
let items_contains_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (item: a * b).{:pattern (items m) item}
(items m) item <==>
FSet.mem (fst item) (domain m)
/\ (elements m) (fst item) == Some (snd item)
/// We represent the following Dafny axiom with `empty_domain_empty_fact`:
///
/// axiom (forall<U, V> u: U ::
/// { Map#Domain(Map#Empty(): Map U V)[u] }
/// !Map#Domain(Map#Empty(): Map U V)[u]);
let empty_domain_empty_fact =
forall (a: eqtype) (b: Type u#b) (u: a).{:pattern FSet.mem u (domain (emptymap #a #b))}
not (FSet.mem u (domain (emptymap #a #b)))
/// We represent the following Dafny axiom with `glue_domain_fact`:
///
/// axiom (forall<U, V> a: [U]bool, b: [U]V, t: Ty ::
/// { Map#Domain(Map#Glue(a, b, t)) }
/// Map#Domain(Map#Glue(a, b, t)) == a);
let glue_domain_fact =
forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).{:pattern domain (glue keys f)}
domain (glue keys f) == keys
/// We represent the following Dafny axiom with `glue_elements_fact`.
/// But we have to change it because our version of `Map#Elements`
/// returns a map to an optional value.
///
/// axiom (forall<U, V> a: [U]bool, b: [U]V, t: Ty ::
/// { Map#Elements(Map#Glue(a, b, t)) }
/// Map#Elements(Map#Glue(a, b, t)) == b);
let glue_elements_fact =
forall (a: eqtype) (b: Type u#b) (keys: FSet.set a) (f: setfun_t a b keys).{:pattern elements (glue keys f)}
domain (glue keys f) == keys
/\ elements (glue keys f) == f
/// We don't need the following Dafny axiom since the type of `glue` implies it:
///
/// axiom (forall a: [Box]bool, b: [Box]Box, t0, t1: Ty ::
/// { Map#Glue(a, b, TMap(t0, t1)) }
/// // In the following line, no trigger needed, since the quantifier only gets used in negative contexts
/// (forall bx: Box :: a[bx] ==> $IsBox(bx, t0) && $IsBox(b[bx], t1))
/// ==>
/// $Is(Map#Glue(a, b, TMap(t0, t1)), TMap(t0, t1)));
/// We represent the following Dafny axiom with `insert_elements_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, u': U, v: V ::
/// { Map#Domain(Map#Build(m, u, v))[u'] } { Map#Elements(Map#Build(m, u, v))[u'] }
/// (u' == u ==> Map#Domain(Map#Build(m, u, v))[u'] &&
/// Map#Elements(Map#Build(m, u, v))[u'] == v) &&
/// (u' != u ==> Map#Domain(Map#Build(m, u, v))[u'] == Map#Domain(m)[u'] &&
/// Map#Elements(Map#Build(m, u, v))[u'] == Map#Elements(m)[u']));
let insert_elements_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (key': a) (value: b).
{:pattern FSet.mem key' (domain (insert key value m)) \/ ((elements (insert key value m)) key')}
(key' = key ==> FSet.mem key' (domain (insert key value m))
/\ (elements (insert key value m)) key' == Some value)
/\ (key' <> key ==> FSet.mem key' (domain (insert key value m)) = FSet.mem key' (domain m)
/\ (elements (insert key value m)) key' == (elements m) key')
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, v: V :: { Map#Card(Map#Build(m, u, v)) }
/// Map#Domain(m)[u] ==> Map#Card(Map#Build(m, u, v)) == Map#Card(m));
let insert_member_cardinality_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (value: b).{:pattern cardinality (insert key value m)}
FSet.mem key (domain m) ==> cardinality (insert key value m) = cardinality m
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<U, V> m: Map U V, u: U, v: V :: { Map#Card(Map#Build(m, u, v)) }
/// !Map#Domain(m)[u] ==> Map#Card(Map#Build(m, u, v)) == Map#Card(m) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (key: a) (value: b).{:pattern cardinality (insert key value m)}
not (FSet.mem key (domain m)) ==> cardinality (insert key value m) = cardinality m + 1
/// We represent the following Dafny axiom with `merge_domain_is_union_fact`:
///
/// axiom (forall<U, V> m: Map U V, n: Map U V ::
/// { Map#Domain(Map#Merge(m, n)) }
/// Map#Domain(Map#Merge(m, n)) == Set#Union(Map#Domain(m), Map#Domain(n)));
let merge_domain_is_union_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern domain (merge m1 m2)}
domain (merge m1 m2) == FSet.union (domain m1) (domain m2)
/// We represent the following Dafny axiom with `merge_element_fact`:
///
/// axiom (forall<U, V> m: Map U V, n: Map U V, u: U ::
/// { Map#Elements(Map#Merge(m, n))[u] }
/// Map#Domain(Map#Merge(m, n))[u] ==>
/// (!Map#Domain(n)[u] ==> Map#Elements(Map#Merge(m, n))[u] == Map#Elements(m)[u]) &&
/// (Map#Domain(n)[u] ==> Map#Elements(Map#Merge(m, n))[u] == Map#Elements(n)[u]));
let merge_element_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b) (key: a).{:pattern (elements (merge m1 m2)) key}
FSet.mem key (domain (merge m1 m2)) ==>
(not (FSet.mem key (domain m2)) ==> FSet.mem key (domain m1) /\ (elements (merge m1 m2)) key == (elements m1) key)
/\ (FSet.mem key (domain m2) ==> (elements (merge m1 m2)) key == (elements m2) key)
/// We represent the following Dafny axiom with `subtract_domain_fact`:
///
/// axiom (forall<U, V> m: Map U V, s: Set U ::
/// { Map#Domain(Map#Subtract(m, s)) }
/// Map#Domain(Map#Subtract(m, s)) == Set#Difference(Map#Domain(m), s));
let subtract_domain_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (s: FSet.set a).{:pattern domain (subtract m s)}
domain (subtract m s) == FSet.difference (domain m) s
/// We represent the following Dafny axiom with `subtract_element_fact`:
///
/// axiom (forall<U, V> m: Map U V, s: Set U, u: U ::
/// { Map#Elements(Map#Subtract(m, s))[u] }
/// Map#Domain(Map#Subtract(m, s))[u] ==>
/// Map#Elements(Map#Subtract(m, s))[u] == Map#Elements(m)[u]);
let subtract_element_fact =
forall (a: eqtype) (b: Type u#b) (m: map a b) (s: FSet.set a) (key: a).{:pattern (elements (subtract m s)) key}
FSet.mem key (domain (subtract m s)) ==> FSet.mem key (domain m) /\ (elements (subtract m s)) key == (elements m) key
/// We represent the following Dafny axiom with `map_equal_fact`:
///
/// axiom (forall<U, V> m: Map U V, m': Map U V::
/// { Map#Equal(m, m') }
/// Map#Equal(m, m') <==> (forall u : U :: Map#Domain(m)[u] == Map#Domain(m')[u]) &&
/// (forall u : U :: Map#Domain(m)[u] ==> Map#Elements(m)[u] == Map#Elements(m')[u]));
let map_equal_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern equal m1 m2}
equal m1 m2 <==> (forall key. FSet.mem key (domain m1) = FSet.mem key (domain m2))
/\ (forall key. FSet.mem key (domain m1) ==> (elements m1) key == (elements m2) key)
/// We represent the following Dafny axiom with `map_extensionality_fact`:
///
/// axiom (forall<U, V> m: Map U V, m': Map U V::
/// { Map#Equal(m, m') }
/// Map#Equal(m, m') ==> m == m');
let map_extensionality_fact =
forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b).{:pattern equal m1 m2}
equal m1 m2 ==> m1 == m2
/// We represent the following Dafny axiom with `disjoint_fact`:
///
/// axiom (forall<U, V> m: Map U V, m': Map U V ::
/// { Map#Disjoint(m, m') }
/// Map#Disjoint(m, m') <==> (forall o: U :: {Map#Domain(m)[o]} {Map#Domain(m')[o]} !Map#Domain(m)[o] || !Map#Domain(m')[o])); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.FiniteSet.Base.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteMap.Base.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FiniteSet.Base",
"short_module": "FSet"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteMap",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteMap",
"short_module": null
},
{
"abbrev": 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.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteMap.Base.map",
"Prims.l_iff",
"FStar.FiniteMap.Base.disjoint",
"Prims.b2t",
"Prims.op_BarBar",
"Prims.op_Negation",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteMap.Base.domain"
] | [] | false | false | false | true | true | let disjoint_fact =
| forall (a: eqtype) (b: Type u#b) (m1: map a b) (m2: map a b). {:pattern disjoint m1 m2}
disjoint m1 m2 <==>
(forall key. {:pattern FSet.mem key (domain m1)\/FSet.mem key (domain m2)}
not (FSet.mem key (domain m1)) || not (FSet.mem key (domain m2))) | false |
|
EverParse3d.Prelude.fst | EverParse3d.Prelude.parse_nlist_total_fixed_size_aux | val parse_nlist_total_fixed_size_aux
(n: U32.t)
(#wk: _)
(#k: parser_kind true wk)
(#t: _)
(p: parser k t)
(x: LP.bytes)
: Lemma
(requires
(let open LP in
k.parser_kind_subkind == Some ParserStrong /\ k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= U32.v n)) (ensures (Some? (LP.parse (parse_nlist n p) x))) | val parse_nlist_total_fixed_size_aux
(n: U32.t)
(#wk: _)
(#k: parser_kind true wk)
(#t: _)
(p: parser k t)
(x: LP.bytes)
: Lemma
(requires
(let open LP in
k.parser_kind_subkind == Some ParserStrong /\ k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= U32.v n)) (ensures (Some? (LP.parse (parse_nlist n p) x))) | let parse_nlist_total_fixed_size_aux
(n:U32.t) (#wk: _) (#k:parser_kind true wk) #t (p:parser k t)
(x: LP.bytes)
: Lemma
(requires (
let open LP in
k.parser_kind_subkind == Some ParserStrong /\
k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= U32.v n
))
(ensures (
Some? (LP.parse (parse_nlist n p) x)
))
= let x' = Seq.slice x 0 (U32.v n) in
let cnt = (U32.v n / k.LP.parser_kind_low) in
FStar.Math.Lemmas.lemma_div_exact (U32.v n) k.LP.parser_kind_low;
FStar.Math.Lemmas.nat_over_pos_is_nat (U32.v n) k.LP.parser_kind_low;
LowParse.Spec.List.parse_list_total_constant_size p cnt x';
LP.parser_kind_prop_equiv LowParse.Spec.List.parse_list_kind (LowParse.Spec.List.parse_list p) | {
"file_name": "src/3d/prelude/EverParse3d.Prelude.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 96,
"end_line": 202,
"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 EverParse3d.Prelude
friend EverParse3d.Kinds
module BF = LowParse.BitFields
module LP = LowParse.Spec.Base
module LPC = LowParse.Spec.Combinators
module LPL = LowParse.Low.Base
module LPLC = LowParse.Low.Combinators
module U16 = FStar.UInt16
module U32 = FStar.UInt32
module U64 = FStar.UInt64
////////////////////////////////////////////////////////////////////////////////
// Parsers
////////////////////////////////////////////////////////////////////////////////
let parser k t = LP.parser k t
let is_weaker_than #nz1 #wk1 (k:parser_kind nz1 wk1)
#nz2 #wk2 (k':parser_kind nz2 wk2) = k `LP.is_weaker_than` k'
let is_weaker_than_refl #nz #wk (k:parser_kind nz wk)
: Lemma (ensures (is_weaker_than k k))
[SMTPat (is_weaker_than k k)]
= ()
let is_weaker_than_glb #nz1 #wk1 (k1:parser_kind nz1 wk1)
#nz2 #wk2 (k2:parser_kind nz2 wk2)
: Lemma (is_weaker_than (glb k1 k2) k1 /\
is_weaker_than (glb k1 k2) k2)
[SMTPatOr
[[SMTPat (is_weaker_than (glb k1 k2) k1)];
[SMTPat (is_weaker_than (glb k1 k2) k2)]]]
= ()
/// Parser: return
inline_for_extraction noextract
let parse_ret #t (v:t)
: Tot (parser ret_kind t)
= LPC.parse_ret #t v
/// Parser: bind
inline_for_extraction noextract
let parse_dep_pair p1 p2
= LPC.parse_dtuple2 p1 p2
/// Parser: sequencing
inline_for_extraction noextract
let parse_pair p1 p2
= LPC.nondep_then p1 p2
/// Parser: map
let injective_map a b = (a -> Tot b) //{LPC.synth_injective f}
inline_for_extraction noextract
let parse_filter p f
= LPC.parse_filter p f
/// Parser: weakening kinds
inline_for_extraction noextract
let parse_weaken #nz #wk (#k:parser_kind nz wk) #t (p:parser k t)
#nz' #wk' (k':parser_kind nz' wk' {k' `is_weaker_than` k})
: Tot (parser k' t)
= LP.weaken k' p
/// Parser: weakening kinds
inline_for_extraction noextract
let parse_weaken_left #nz #wk #k p k'
= LP.weaken (glb k' k) p
/// Parser: weakening kinds
inline_for_extraction noextract
let parse_weaken_right #nz #wk #k p k'
= LP.weaken (glb k k') p
/// Parser: unreachable, for default cases of exhaustive pattern matching
inline_for_extraction noextract
let parse_impos ()
: parser impos_kind False
= let p : LP.bare_parser False = fun b -> None in
LP.parser_kind_prop_equiv impos_kind p;
p
let parse_ite e p1 p2
= if e then p1 () else p2 ()
let nlist (n:U32.t) (t:Type) = list t
inline_for_extraction noextract
let parse_nlist n #wk #k #t p
= let open LowParse.Spec.FLData in
let open LowParse.Spec.List in
parse_weaken
#false #WeakKindStrongPrefix #(parse_fldata_kind (U32.v n) parse_list_kind) #(list t)
(LowParse.Spec.FLData.parse_fldata (LowParse.Spec.List.parse_list p) (U32.v n))
#false kind_nlist
let all_bytes = Seq.seq LP.byte
let parse_all_bytes'
: LP.bare_parser all_bytes
= fun input -> Some (input, (Seq.length input <: LP.consumed_length input))
let parse_all_bytes =
LP.parser_kind_prop_equiv kind_all_bytes parse_all_bytes';
parse_all_bytes'
////////////////////////////////////////////////////////////////////////////////
module B32 = FStar.Bytes
let t_at_most (n:U32.t) (t:Type) = t & all_bytes
inline_for_extraction noextract
let parse_t_at_most n #nz #wk #k #t p
= let open LowParse.Spec.FLData in
let open LowParse.Spec.List in
parse_weaken
#false
#WeakKindStrongPrefix
(LowParse.Spec.FLData.parse_fldata
(LPC.nondep_then p parse_all_bytes)
(U32.v n))
#false
kind_t_at_most
////////////////////////////////////////////////////////////////////////////////
let t_exact (n:U32.t) (t:Type) = t
inline_for_extraction noextract
let parse_t_exact n #nz #wk #k #t p
= let open LowParse.Spec.FLData in
let open LowParse.Spec.List in
parse_weaken
#false
#WeakKindStrongPrefix
(LowParse.Spec.FLData.parse_fldata
p
(U32.v n))
#false
kind_t_exact
////////////////////////////////////////////////////////////////////////////////
// Readers
////////////////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
let reader p = LPLC.leaf_reader p
inline_for_extraction noextract
let read_filter p32 f
= LPLC.read_filter p32 f
let read_impos : reader (parse_impos()) =
fun #rrel #rel sl pos ->
let h = FStar.HyperStack.ST.get() in
assert (LPLC.valid (parse_impos()) h sl pos);
LowParse.Low.Base.Spec.valid_equiv (parse_impos()) h sl pos;
false_elim ()
// ////////////////////////////////////////////////////////////////////////////////
// // Validators
// ////////////////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
let validator #nz #wk (#k:parser_kind nz wk) (#t:Type) (p:parser k t)
: Type
= LPL.validator #k #t p
inline_for_extraction noextract
let validator_no_read #nz #wk (#k:parser_kind nz wk) (#t:Type) (p:parser k t)
: Type
= LPL.validator_no_read #k #t p | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.ListUpTo.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"LowParse.Spec.Base.fsti.checked",
"LowParse.Low.Int.fsti.checked",
"LowParse.Low.Combinators.fsti.checked",
"LowParse.Low.BoundedInt.fsti.checked",
"LowParse.Low.Base.Spec.fsti.checked",
"LowParse.Low.Base.fst.checked",
"LowParse.BitFields.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Bytes.fsti.checked",
"EverParse3d.Kinds.fst.checked"
],
"interface_file": true,
"source_file": "EverParse3d.Prelude.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Bytes",
"short_module": "B32"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Combinators",
"short_module": "LPLC"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Base",
"short_module": "LPL"
},
{
"abbrev": true,
"full_module": "LowParse.Spec.Combinators",
"short_module": "LPC"
},
{
"abbrev": true,
"full_module": "LowParse.Spec.Base",
"short_module": "LP"
},
{
"abbrev": true,
"full_module": "LowParse.BitFields",
"short_module": "BF"
},
{
"abbrev": true,
"full_module": "FStar.Int.Cast",
"short_module": "C"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": false,
"full_module": "FStar.Range",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.Kinds",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.Prelude.StaticHeader",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d",
"short_module": null
},
{
"abbrev": 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": 2,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [
"smt.qi.eager_threshold=10"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: FStar.UInt32.t -> p: EverParse3d.Prelude.parser k t -> x: LowParse.Bytes.bytes
-> FStar.Pervasives.Lemma
(requires
Mkparser_kind'?.parser_kind_subkind k ==
FStar.Pervasives.Native.Some LowParse.Spec.Base.ParserStrong /\
Mkparser_kind'?.parser_kind_high k ==
FStar.Pervasives.Native.Some (Mkparser_kind'?.parser_kind_low k) /\
FStar.UInt32.v n % Mkparser_kind'?.parser_kind_low k == 0 /\
Mkparser_kind'?.parser_kind_metadata k ==
FStar.Pervasives.Native.Some LowParse.Spec.Base.ParserKindMetadataTotal /\
FStar.Seq.Base.length x >= FStar.UInt32.v n)
(ensures Some? (LowParse.Spec.Base.parse (EverParse3d.Prelude.parse_nlist n p) x)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt32.t",
"EverParse3d.Kinds.weak_kind",
"EverParse3d.Kinds.parser_kind",
"EverParse3d.Prelude.parser",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"Prims.list",
"LowParse.Spec.List.parse_list_kind",
"LowParse.Spec.List.parse_list",
"Prims.unit",
"LowParse.Spec.List.parse_list_total_constant_size",
"FStar.Math.Lemmas.nat_over_pos_is_nat",
"FStar.UInt32.v",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"FStar.Math.Lemmas.lemma_div_exact",
"Prims.int",
"Prims.op_Division",
"FStar.Seq.Base.seq",
"LowParse.Bytes.byte",
"FStar.Seq.Base.slice",
"Prims.l_and",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"LowParse.Spec.Base.parser_subkind",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_subkind",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.ParserStrong",
"Prims.nat",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high",
"Prims.op_Modulus",
"LowParse.Spec.Base.parser_kind_metadata_some",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata",
"LowParse.Spec.Base.ParserKindMetadataTotal",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.Seq.Base.length",
"Prims.squash",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.Pervasives.Native.tuple2",
"EverParse3d.Prelude.nlist",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"EverParse3d.Prelude.parse_nlist",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let parse_nlist_total_fixed_size_aux
(n: U32.t)
(#wk: _)
(#k: parser_kind true wk)
#t
(p: parser k t)
(x: LP.bytes)
: Lemma
(requires
(let open LP in
k.parser_kind_subkind == Some ParserStrong /\ k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\ k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= U32.v n)) (ensures (Some? (LP.parse (parse_nlist n p) x))) =
| let x' = Seq.slice x 0 (U32.v n) in
let cnt = (U32.v n / k.LP.parser_kind_low) in
FStar.Math.Lemmas.lemma_div_exact (U32.v n) k.LP.parser_kind_low;
FStar.Math.Lemmas.nat_over_pos_is_nat (U32.v n) k.LP.parser_kind_low;
LowParse.Spec.List.parse_list_total_constant_size p cnt x';
LP.parser_kind_prop_equiv LowParse.Spec.List.parse_list_kind (LowParse.Spec.List.parse_list p) | false |
STLC.Core.fst | STLC.Core.open_exp' | val open_exp' (e: stlc_exp) (v: var) (n: index) : e': stlc_exp{size e == size e'} | val open_exp' (e: stlc_exp) (v: var) (n: index) : e': stlc_exp{size e == size e'} | let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n) | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 62,
"end_line": 49,
"start_col": 0,
"start_line": 42
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: STLC.Core.stlc_exp -> v: STLC.Core.var -> n: STLC.Core.index
-> e': STLC.Core.stlc_exp{STLC.Core.size e == STLC.Core.size e'} | Prims.Tot | [
"total"
] | [] | [
"STLC.Core.stlc_exp",
"STLC.Core.var",
"STLC.Core.index",
"STLC.Core.EUnit",
"STLC.Core.EVar",
"Prims.op_Equality",
"Prims.bool",
"STLC.Core.EBVar",
"Prims.eq2",
"Prims.nat",
"STLC.Core.size",
"STLC.Core.stlc_ty",
"STLC.Core.ELam",
"STLC.Core.open_exp'",
"Prims.op_Addition",
"STLC.Core.EApp"
] | [
"recursion"
] | false | false | false | false | false | let rec open_exp' (e: stlc_exp) (v: var) (n: index) : e': stlc_exp{size e == size e'} =
| match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n) | false |
STLC.Core.fst | STLC.Core.lookup | val lookup (e: list (var & 'a)) (x: var) : option 'a | val lookup (e: list (var & 'a)) (x: var) : option 'a | let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 15,
"end_line": 143,
"start_col": 0,
"start_line": 141
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: Prims.list (STLC.Core.var * 'a) -> x: STLC.Core.var -> FStar.Pervasives.Native.option 'a | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.var",
"FStar.List.Tot.Base.assoc",
"FStar.Pervasives.Native.option"
] | [] | false | false | false | true | false | let lookup (e: list (var & 'a)) (x: var) : option 'a =
| L.assoc x e | false |
EverParse3d.Prelude.fst | EverParse3d.Prelude.parse_nlist_total_fixed_size_kind_correct | val parse_nlist_total_fixed_size_kind_correct
(n: U32.t)
(#wk: _)
(#k: parser_kind true wk)
(#t: _)
(p: parser k t)
: Lemma
(requires
(let open LP in
k.parser_kind_subkind == Some ParserStrong /\ k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\ k.parser_kind_metadata == Some ParserKindMetadataTotal
))
(ensures
(LP.parser_kind_prop (LP.total_constant_size_parser_kind (U32.v n)) (parse_nlist n p))) | val parse_nlist_total_fixed_size_kind_correct
(n: U32.t)
(#wk: _)
(#k: parser_kind true wk)
(#t: _)
(p: parser k t)
: Lemma
(requires
(let open LP in
k.parser_kind_subkind == Some ParserStrong /\ k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\ k.parser_kind_metadata == Some ParserKindMetadataTotal
))
(ensures
(LP.parser_kind_prop (LP.total_constant_size_parser_kind (U32.v n)) (parse_nlist n p))) | let parse_nlist_total_fixed_size_kind_correct
(n:U32.t) (#wk: _) (#k:parser_kind true wk) #t (p:parser k t)
: Lemma
(requires (
let open LP in
k.parser_kind_subkind == Some ParserStrong /\
k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\
k.parser_kind_metadata == Some ParserKindMetadataTotal
))
(ensures (
LP.parser_kind_prop (LP.total_constant_size_parser_kind (U32.v n)) (parse_nlist n p)
))
= LP.parser_kind_prop_equiv (LowParse.Spec.FLData.parse_fldata_kind (U32.v n) LowParse.Spec.List.parse_list_kind) (parse_nlist n p);
LP.parser_kind_prop_equiv (LP.total_constant_size_parser_kind (U32.v n)) (parse_nlist n p);
Classical.forall_intro (Classical.move_requires (parse_nlist_total_fixed_size_aux n p)) | {
"file_name": "src/3d/prelude/EverParse3d.Prelude.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 89,
"end_line": 219,
"start_col": 0,
"start_line": 204
} | (*
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 EverParse3d.Prelude
friend EverParse3d.Kinds
module BF = LowParse.BitFields
module LP = LowParse.Spec.Base
module LPC = LowParse.Spec.Combinators
module LPL = LowParse.Low.Base
module LPLC = LowParse.Low.Combinators
module U16 = FStar.UInt16
module U32 = FStar.UInt32
module U64 = FStar.UInt64
////////////////////////////////////////////////////////////////////////////////
// Parsers
////////////////////////////////////////////////////////////////////////////////
let parser k t = LP.parser k t
let is_weaker_than #nz1 #wk1 (k:parser_kind nz1 wk1)
#nz2 #wk2 (k':parser_kind nz2 wk2) = k `LP.is_weaker_than` k'
let is_weaker_than_refl #nz #wk (k:parser_kind nz wk)
: Lemma (ensures (is_weaker_than k k))
[SMTPat (is_weaker_than k k)]
= ()
let is_weaker_than_glb #nz1 #wk1 (k1:parser_kind nz1 wk1)
#nz2 #wk2 (k2:parser_kind nz2 wk2)
: Lemma (is_weaker_than (glb k1 k2) k1 /\
is_weaker_than (glb k1 k2) k2)
[SMTPatOr
[[SMTPat (is_weaker_than (glb k1 k2) k1)];
[SMTPat (is_weaker_than (glb k1 k2) k2)]]]
= ()
/// Parser: return
inline_for_extraction noextract
let parse_ret #t (v:t)
: Tot (parser ret_kind t)
= LPC.parse_ret #t v
/// Parser: bind
inline_for_extraction noextract
let parse_dep_pair p1 p2
= LPC.parse_dtuple2 p1 p2
/// Parser: sequencing
inline_for_extraction noextract
let parse_pair p1 p2
= LPC.nondep_then p1 p2
/// Parser: map
let injective_map a b = (a -> Tot b) //{LPC.synth_injective f}
inline_for_extraction noextract
let parse_filter p f
= LPC.parse_filter p f
/// Parser: weakening kinds
inline_for_extraction noextract
let parse_weaken #nz #wk (#k:parser_kind nz wk) #t (p:parser k t)
#nz' #wk' (k':parser_kind nz' wk' {k' `is_weaker_than` k})
: Tot (parser k' t)
= LP.weaken k' p
/// Parser: weakening kinds
inline_for_extraction noextract
let parse_weaken_left #nz #wk #k p k'
= LP.weaken (glb k' k) p
/// Parser: weakening kinds
inline_for_extraction noextract
let parse_weaken_right #nz #wk #k p k'
= LP.weaken (glb k k') p
/// Parser: unreachable, for default cases of exhaustive pattern matching
inline_for_extraction noextract
let parse_impos ()
: parser impos_kind False
= let p : LP.bare_parser False = fun b -> None in
LP.parser_kind_prop_equiv impos_kind p;
p
let parse_ite e p1 p2
= if e then p1 () else p2 ()
let nlist (n:U32.t) (t:Type) = list t
inline_for_extraction noextract
let parse_nlist n #wk #k #t p
= let open LowParse.Spec.FLData in
let open LowParse.Spec.List in
parse_weaken
#false #WeakKindStrongPrefix #(parse_fldata_kind (U32.v n) parse_list_kind) #(list t)
(LowParse.Spec.FLData.parse_fldata (LowParse.Spec.List.parse_list p) (U32.v n))
#false kind_nlist
let all_bytes = Seq.seq LP.byte
let parse_all_bytes'
: LP.bare_parser all_bytes
= fun input -> Some (input, (Seq.length input <: LP.consumed_length input))
let parse_all_bytes =
LP.parser_kind_prop_equiv kind_all_bytes parse_all_bytes';
parse_all_bytes'
////////////////////////////////////////////////////////////////////////////////
module B32 = FStar.Bytes
let t_at_most (n:U32.t) (t:Type) = t & all_bytes
inline_for_extraction noextract
let parse_t_at_most n #nz #wk #k #t p
= let open LowParse.Spec.FLData in
let open LowParse.Spec.List in
parse_weaken
#false
#WeakKindStrongPrefix
(LowParse.Spec.FLData.parse_fldata
(LPC.nondep_then p parse_all_bytes)
(U32.v n))
#false
kind_t_at_most
////////////////////////////////////////////////////////////////////////////////
let t_exact (n:U32.t) (t:Type) = t
inline_for_extraction noextract
let parse_t_exact n #nz #wk #k #t p
= let open LowParse.Spec.FLData in
let open LowParse.Spec.List in
parse_weaken
#false
#WeakKindStrongPrefix
(LowParse.Spec.FLData.parse_fldata
p
(U32.v n))
#false
kind_t_exact
////////////////////////////////////////////////////////////////////////////////
// Readers
////////////////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
let reader p = LPLC.leaf_reader p
inline_for_extraction noextract
let read_filter p32 f
= LPLC.read_filter p32 f
let read_impos : reader (parse_impos()) =
fun #rrel #rel sl pos ->
let h = FStar.HyperStack.ST.get() in
assert (LPLC.valid (parse_impos()) h sl pos);
LowParse.Low.Base.Spec.valid_equiv (parse_impos()) h sl pos;
false_elim ()
// ////////////////////////////////////////////////////////////////////////////////
// // Validators
// ////////////////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
let validator #nz #wk (#k:parser_kind nz wk) (#t:Type) (p:parser k t)
: Type
= LPL.validator #k #t p
inline_for_extraction noextract
let validator_no_read #nz #wk (#k:parser_kind nz wk) (#t:Type) (p:parser k t)
: Type
= LPL.validator_no_read #k #t p
let parse_nlist_total_fixed_size_aux
(n:U32.t) (#wk: _) (#k:parser_kind true wk) #t (p:parser k t)
(x: LP.bytes)
: Lemma
(requires (
let open LP in
k.parser_kind_subkind == Some ParserStrong /\
k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= U32.v n
))
(ensures (
Some? (LP.parse (parse_nlist n p) x)
))
= let x' = Seq.slice x 0 (U32.v n) in
let cnt = (U32.v n / k.LP.parser_kind_low) in
FStar.Math.Lemmas.lemma_div_exact (U32.v n) k.LP.parser_kind_low;
FStar.Math.Lemmas.nat_over_pos_is_nat (U32.v n) k.LP.parser_kind_low;
LowParse.Spec.List.parse_list_total_constant_size p cnt x';
LP.parser_kind_prop_equiv LowParse.Spec.List.parse_list_kind (LowParse.Spec.List.parse_list p) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.ListUpTo.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"LowParse.Spec.Base.fsti.checked",
"LowParse.Low.Int.fsti.checked",
"LowParse.Low.Combinators.fsti.checked",
"LowParse.Low.BoundedInt.fsti.checked",
"LowParse.Low.Base.Spec.fsti.checked",
"LowParse.Low.Base.fst.checked",
"LowParse.BitFields.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Bytes.fsti.checked",
"EverParse3d.Kinds.fst.checked"
],
"interface_file": true,
"source_file": "EverParse3d.Prelude.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Bytes",
"short_module": "B32"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Combinators",
"short_module": "LPLC"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Base",
"short_module": "LPL"
},
{
"abbrev": true,
"full_module": "LowParse.Spec.Combinators",
"short_module": "LPC"
},
{
"abbrev": true,
"full_module": "LowParse.Spec.Base",
"short_module": "LP"
},
{
"abbrev": true,
"full_module": "LowParse.BitFields",
"short_module": "BF"
},
{
"abbrev": true,
"full_module": "FStar.Int.Cast",
"short_module": "C"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": false,
"full_module": "FStar.Range",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.Kinds",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.Prelude.StaticHeader",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d",
"short_module": null
},
{
"abbrev": 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": 2,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [
"smt.qi.eager_threshold=10"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: FStar.UInt32.t -> p: EverParse3d.Prelude.parser k t
-> FStar.Pervasives.Lemma
(requires
Mkparser_kind'?.parser_kind_subkind k ==
FStar.Pervasives.Native.Some LowParse.Spec.Base.ParserStrong /\
Mkparser_kind'?.parser_kind_high k ==
FStar.Pervasives.Native.Some (Mkparser_kind'?.parser_kind_low k) /\
FStar.UInt32.v n % Mkparser_kind'?.parser_kind_low k == 0 /\
Mkparser_kind'?.parser_kind_metadata k ==
FStar.Pervasives.Native.Some LowParse.Spec.Base.ParserKindMetadataTotal)
(ensures
LowParse.Spec.Base.parser_kind_prop (LowParse.Spec.Base.total_constant_size_parser_kind (FStar.UInt32.v
n))
(EverParse3d.Prelude.parse_nlist n p)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt32.t",
"EverParse3d.Kinds.weak_kind",
"EverParse3d.Kinds.parser_kind",
"EverParse3d.Prelude.parser",
"FStar.Classical.forall_intro",
"LowParse.Bytes.bytes",
"Prims.l_imp",
"Prims.l_and",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"LowParse.Spec.Base.parser_subkind",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_subkind",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.ParserStrong",
"Prims.nat",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.int",
"Prims.op_Modulus",
"FStar.UInt32.v",
"LowParse.Spec.Base.parser_kind_metadata_some",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata",
"LowParse.Spec.Base.ParserKindMetadataTotal",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.Pervasives.Native.tuple2",
"EverParse3d.Prelude.nlist",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"EverParse3d.Prelude.parse_nlist",
"FStar.Classical.move_requires",
"EverParse3d.Prelude.parse_nlist_total_fixed_size_aux",
"Prims.unit",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"LowParse.Spec.Base.total_constant_size_parser_kind",
"LowParse.Spec.FLData.parse_fldata_kind",
"LowParse.Spec.List.parse_list_kind",
"Prims.squash",
"LowParse.Spec.Base.parser_kind_prop",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let parse_nlist_total_fixed_size_kind_correct
(n: U32.t)
(#wk: _)
(#k: parser_kind true wk)
#t
(p: parser k t)
: Lemma
(requires
(let open LP in
k.parser_kind_subkind == Some ParserStrong /\ k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\ k.parser_kind_metadata == Some ParserKindMetadataTotal
))
(ensures
(LP.parser_kind_prop (LP.total_constant_size_parser_kind (U32.v n)) (parse_nlist n p))) =
| LP.parser_kind_prop_equiv (LowParse.Spec.FLData.parse_fldata_kind (U32.v n)
LowParse.Spec.List.parse_list_kind)
(parse_nlist n p);
LP.parser_kind_prop_equiv (LP.total_constant_size_parser_kind (U32.v n)) (parse_nlist n p);
Classical.forall_intro (Classical.move_requires (parse_nlist_total_fixed_size_aux n p)) | false |
STLC.Core.fst | STLC.Core.fresh | val fresh (e: list (var & 'a)) : var | val fresh (e: list (var & 'a)) : var | let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1 | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 33,
"end_line": 152,
"start_col": 0,
"start_line": 147
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: Prims.list (STLC.Core.var * 'a) -> STLC.Core.var | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.var",
"Prims.op_Addition",
"STLC.Core.max",
"STLC.Core.fresh",
"FStar.Pervasives.Native.fst"
] | [
"recursion"
] | false | false | false | true | false | let rec fresh (e: list (var & 'a)) : var =
| match e with
| [] -> 0
| hd :: tl -> max (fresh tl) (fst hd) + 1 | false |
STLC.Core.fst | STLC.Core.fresh_is_fresh | val fresh_is_fresh (e: list (var & 'a)) : Lemma (None? (lookup e (fresh e))) | val fresh_is_fresh (e: list (var & 'a)) : Lemma (None? (lookup e (fresh e))) | let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e) | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 53,
"end_line": 175,
"start_col": 0,
"start_line": 169
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: Prims.list (STLC.Core.var * 'a)
-> FStar.Pervasives.Lemma (ensures None? (STLC.Core.lookup e (STLC.Core.fresh e))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.var",
"STLC.Core.lookup",
"STLC.Core.fresh",
"FStar.Classical.forall_intro",
"Prims.l_imp",
"FStar.List.Tot.Base.memP",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.Pervasives.Native.fst",
"STLC.Core.fresh_not_mem",
"Prims.unit",
"STLC.Core.lookup_mem",
"Prims.l_True",
"Prims.squash",
"FStar.Pervasives.Native.uu___is_None",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let fresh_is_fresh (e: list (var & 'a)) : Lemma (None? (lookup e (fresh e))) =
| match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e) | false |
STLC.Core.fst | STLC.Core.freevars_open | val freevars_open (e: stlc_exp) (x: var) (n: nat)
: Lemma ((freevars (open_exp' e x n)) `Set.subset` ((freevars e) `Set.union` (Set.singleton x))) | val freevars_open (e: stlc_exp) (x: var) (n: nat)
: Lemma ((freevars (open_exp' e x n)) `Set.subset` ((freevars e) `Set.union` (Set.singleton x))) | let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 26,
"end_line": 137,
"start_col": 0,
"start_line": 127
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: STLC.Core.stlc_exp -> x: STLC.Core.var -> n: Prims.nat
-> FStar.Pervasives.Lemma
(ensures
FStar.Set.subset (STLC.Core.freevars (STLC.Core.open_exp' e x n))
(FStar.Set.union (STLC.Core.freevars e) (FStar.Set.singleton x))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"STLC.Core.stlc_exp",
"STLC.Core.var",
"Prims.nat",
"STLC.Core.index",
"STLC.Core.stlc_ty",
"STLC.Core.freevars_open",
"Prims.op_Addition",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"FStar.Set.subset",
"STLC.Core.freevars",
"STLC.Core.open_exp'",
"FStar.Set.union",
"FStar.Set.singleton",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec freevars_open (e: stlc_exp) (x: var) (n: nat)
: Lemma ((freevars (open_exp' e x n)) `Set.subset` ((freevars e) `Set.union` (Set.singleton x))) =
| match e with
| EUnit | EBVar _ | EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n | false |
STLC.Core.fst | STLC.Core.elab_exp | val elab_exp (e: stlc_exp) : Tot R.term (decreases (size e)) | val elab_exp (e: stlc_exp) : Tot R.term (decreases (size e)) | let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit)) | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 44,
"end_line": 320,
"start_col": 0,
"start_line": 297
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: STLC.Core.stlc_exp -> Prims.Tot FStar.Stubs.Reflection.Types.term | Prims.Tot | [
"total",
""
] | [] | [
"STLC.Core.stlc_exp",
"FStar.Stubs.Reflection.V2.Builtins.pack_ln",
"FStar.Stubs.Reflection.V2.Data.Tv_Const",
"FStar.Stubs.Reflection.V2.Data.C_Unit",
"STLC.Core.index",
"FStar.Stubs.Reflection.V2.Data.Tv_BVar",
"FStar.Stubs.Reflection.Types.bv",
"FStar.Stubs.Reflection.V2.Builtins.pack_bv",
"FStar.Reflection.Typing.make_bv",
"STLC.Core.var",
"FStar.Stubs.Reflection.V2.Data.Tv_Var",
"FStar.Stubs.Reflection.Types.namedv",
"FStar.Stubs.Reflection.V2.Builtins.pack_namedv",
"FStar.Reflection.Typing.make_namedv",
"STLC.Core.stlc_ty",
"FStar.Stubs.Reflection.V2.Data.Tv_Abs",
"FStar.Reflection.Typing.mk_simple_binder",
"FStar.Reflection.Typing.pp_name_default",
"FStar.Stubs.Reflection.Types.term",
"STLC.Core.elab_exp",
"STLC.Core.elab_ty",
"FStar.Stubs.Reflection.V2.Data.Tv_App",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Stubs.Reflection.V2.Data.aqualv",
"FStar.Stubs.Reflection.V2.Data.Q_Explicit"
] | [
"recursion"
] | false | false | false | true | false | let rec elab_exp (e: stlc_exp) : Tot R.term (decreases (size e)) =
| let open R in
match e with
| EUnit -> pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit)) | false |
STLC.Core.fst | STLC.Core.freevars | val freevars (e: stlc_exp) : Set.set var | val freevars (e: stlc_exp) : Set.set var | let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2 | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 55,
"end_line": 114,
"start_col": 0,
"start_line": 107
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: STLC.Core.stlc_exp -> FStar.Set.set STLC.Core.var | Prims.Tot | [
"total"
] | [] | [
"STLC.Core.stlc_exp",
"FStar.Set.empty",
"STLC.Core.var",
"STLC.Core.index",
"FStar.Set.singleton",
"STLC.Core.stlc_ty",
"STLC.Core.freevars",
"FStar.Set.union",
"FStar.Set.set"
] | [
"recursion"
] | false | false | false | true | false | let rec freevars (e: stlc_exp) : Set.set var =
| match e with
| EUnit | EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> (freevars e1) `Set.union` (freevars e2) | false |
STLC.Core.fst | STLC.Core.stlc_types_are_closed1 | val stlc_types_are_closed1 (ty: stlc_ty) (v: R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty) [SMTPat (RT.open_with (elab_ty ty) v)] | val stlc_types_are_closed1 (ty: stlc_ty) (v: R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty) [SMTPat (RT.open_with (elab_ty ty) v)] | let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 357,
"start_col": 0,
"start_line": 353
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | ty: STLC.Core.stlc_ty -> v: FStar.Stubs.Reflection.Types.term
-> FStar.Pervasives.Lemma
(ensures FStar.Reflection.Typing.open_with (STLC.Core.elab_ty ty) v == STLC.Core.elab_ty ty)
[SMTPat (FStar.Reflection.Typing.open_with (STLC.Core.elab_ty ty) v)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"STLC.Core.stlc_ty",
"FStar.Stubs.Reflection.Types.term",
"FStar.Reflection.Typing.open_with_spec",
"STLC.Core.elab_ty",
"Prims.unit",
"STLC.Core.stlc_types_are_closed_core",
"Prims.Cons",
"FStar.Reflection.Typing.subst_elt",
"FStar.Reflection.Typing.DT",
"Prims.Nil",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.Reflection.Typing.open_with",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat"
] | [] | true | false | true | false | false | let stlc_types_are_closed1 (ty: stlc_ty) (v: R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty) [SMTPat (RT.open_with (elab_ty ty) v)] =
| stlc_types_are_closed_core ty [RT.DT 0 v];
RT.open_with_spec (elab_ty ty) v | false |
STLC.Core.fst | STLC.Core.lookup_mem | val lookup_mem (e: list (var & 'a)) (x: var)
: Lemma (requires Some? (lookup e x)) (ensures exists elt. L.memP elt e /\ fst elt == x) | val lookup_mem (e: list (var & 'a)) (x: var)
: Lemma (requires Some? (lookup e x)) (ensures exists elt. L.memP elt e /\ fst elt == x) | let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 45,
"end_line": 167,
"start_col": 0,
"start_line": 162
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: Prims.list (STLC.Core.var * 'a) -> x: STLC.Core.var
-> FStar.Pervasives.Lemma (requires Some? (STLC.Core.lookup e x))
(ensures
exists (elt: (STLC.Core.var * 'a)).
FStar.List.Tot.Base.memP elt e /\ FStar.Pervasives.Native.fst elt == x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.var",
"FStar.List.Tot.Properties.assoc_memP_some",
"Prims.unit",
"FStar.Pervasives.Native.option",
"STLC.Core.lookup",
"Prims.b2t",
"FStar.Pervasives.Native.uu___is_Some",
"Prims.squash",
"Prims.l_Exists",
"Prims.l_and",
"FStar.List.Tot.Base.memP",
"Prims.eq2",
"FStar.Pervasives.Native.fst",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let lookup_mem (e: list (var & 'a)) (x: var)
: Lemma (requires Some? (lookup e x)) (ensures exists elt. L.memP elt e /\ fst elt == x) =
| let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.parse_bcvli_payload_kind | val parse_bcvli_payload_kind : LowParse.Spec.Base.parser_kind' | let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
} | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 13,
"start_col": 0,
"start_line": 8
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | LowParse.Spec.Base.parser_kind' | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.Mkparser_kind'",
"FStar.Pervasives.Native.Some",
"Prims.nat",
"LowParse.Spec.Base.parser_subkind",
"LowParse.Spec.Base.ParserStrong",
"FStar.Pervasives.Native.None",
"LowParse.Spec.Base.parser_kind_metadata_some"
] | [] | false | false | false | true | false | let parse_bcvli_payload_kind =
| {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None
} | false |
|
STLC.Core.fst | STLC.Core.vars_of_env | val vars_of_env (sg: list (var & stlc_ty)) : GTot (Set.set var) | val vars_of_env (sg: list (var & stlc_ty)) : GTot (Set.set var) | let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg) | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 31,
"end_line": 236,
"start_col": 0,
"start_line": 234
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | sg: Prims.list (STLC.Core.var * STLC.Core.stlc_ty) -> Prims.GTot (FStar.Set.set STLC.Core.var) | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.var",
"STLC.Core.stlc_ty",
"FStar.Set.intension",
"STLC.Core.contains",
"FStar.Set.set"
] | [] | false | false | false | false | false | let vars_of_env (sg: list (var & stlc_ty)) : GTot (Set.set var) =
| Set.intension (contains sg) | false |
STLC.Core.fst | STLC.Core.close_exp' | val close_exp' (e: stlc_exp) (v: var) (n: nat) : e': stlc_exp{size e == size e'} | val close_exp' (e: stlc_exp) (v: var) (n: nat) : e': stlc_exp{size e == size e'} | let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n) | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 64,
"end_line": 58,
"start_col": 0,
"start_line": 51
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: STLC.Core.stlc_exp -> v: STLC.Core.var -> n: Prims.nat
-> e': STLC.Core.stlc_exp{STLC.Core.size e == STLC.Core.size e'} | Prims.Tot | [
"total"
] | [] | [
"STLC.Core.stlc_exp",
"STLC.Core.var",
"Prims.nat",
"STLC.Core.EUnit",
"Prims.op_Equality",
"STLC.Core.EBVar",
"Prims.bool",
"STLC.Core.EVar",
"Prims.eq2",
"STLC.Core.size",
"STLC.Core.index",
"STLC.Core.stlc_ty",
"STLC.Core.ELam",
"STLC.Core.close_exp'",
"Prims.op_Addition",
"STLC.Core.EApp"
] | [
"recursion"
] | false | false | false | false | false | let rec close_exp' (e: stlc_exp) (v: var) (n: nat) : e': stlc_exp{size e == size e'} =
| match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n) | false |
STLC.Core.fst | STLC.Core.stlc_types_are_closed_core | val stlc_types_are_closed_core (ty: stlc_ty) (ss: RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)] | val stlc_types_are_closed_core (ty: stlc_ty) (ss: RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)] | let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss) | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 55,
"end_line": 351,
"start_col": 0,
"start_line": 342
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | ty: STLC.Core.stlc_ty -> ss: FStar.Reflection.Typing.subst
-> FStar.Pervasives.Lemma
(ensures FStar.Reflection.Typing.subst_term (STLC.Core.elab_ty ty) ss == STLC.Core.elab_ty ty)
(decreases ty)
[SMTPat (FStar.Reflection.Typing.subst_term (STLC.Core.elab_ty ty) ss)] | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"STLC.Core.stlc_ty",
"FStar.Reflection.Typing.subst",
"STLC.Core.stlc_types_are_closed_core",
"FStar.Reflection.Typing.shift_subst",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.Stubs.Reflection.Types.term",
"FStar.Reflection.Typing.subst_term",
"STLC.Core.elab_ty",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [
"recursion"
] | false | false | true | false | false | let rec stlc_types_are_closed_core (ty: stlc_ty) (ss: RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)] =
| match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss) | false |
STLC.Core.fst | STLC.Core.stlc_types_are_closed2 | val stlc_types_are_closed2 (ty: stlc_ty) (x: R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty) [SMTPat (RT.close_term (elab_ty ty) x)] | val stlc_types_are_closed2 (ty: stlc_ty) (x: R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty) [SMTPat (RT.close_term (elab_ty ty) x)] | let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 363,
"start_col": 0,
"start_line": 359
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | ty: STLC.Core.stlc_ty -> x: FStar.Stubs.Reflection.V2.Data.var
-> FStar.Pervasives.Lemma
(ensures FStar.Reflection.Typing.close_term (STLC.Core.elab_ty ty) x == STLC.Core.elab_ty ty)
[SMTPat (FStar.Reflection.Typing.close_term (STLC.Core.elab_ty ty) x)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"STLC.Core.stlc_ty",
"FStar.Stubs.Reflection.V2.Data.var",
"FStar.Reflection.Typing.close_term_spec",
"STLC.Core.elab_ty",
"Prims.unit",
"STLC.Core.stlc_types_are_closed_core",
"Prims.Cons",
"FStar.Reflection.Typing.subst_elt",
"FStar.Reflection.Typing.ND",
"Prims.Nil",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.Stubs.Reflection.Types.term",
"FStar.Reflection.Typing.close_term",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat"
] | [] | true | false | true | false | false | let stlc_types_are_closed2 (ty: stlc_ty) (x: R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty) [SMTPat (RT.close_term (elab_ty ty) x)] =
| stlc_types_are_closed_core ty [RT.ND x 0];
RT.close_term_spec (elab_ty ty) x | false |
STLC.Core.fst | STLC.Core.closed | val closed (e: stlc_exp) : b: bool{b <==> ((freevars e) `Set.equal` Set.empty)} | val closed (e: stlc_exp) : b: bool{b <==> ((freevars e) `Set.equal` Set.empty)} | let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2 | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 42,
"end_line": 125,
"start_col": 0,
"start_line": 116
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: STLC.Core.stlc_exp
-> b: Prims.bool{b <==> FStar.Set.equal (STLC.Core.freevars e) FStar.Set.empty} | Prims.Tot | [
"total"
] | [] | [
"STLC.Core.stlc_exp",
"STLC.Core.index",
"STLC.Core.var",
"Prims.unit",
"Prims._assert",
"Prims.b2t",
"FStar.Set.mem",
"STLC.Core.freevars",
"STLC.Core.stlc_ty",
"STLC.Core.closed",
"Prims.op_AmpAmp",
"Prims.bool",
"Prims.l_iff",
"FStar.Set.equal",
"FStar.Set.empty"
] | [
"recursion"
] | false | false | false | false | false | let rec closed (e: stlc_exp) : b: bool{b <==> ((freevars e) `Set.equal` Set.empty)} =
| match e with
| EUnit | EBVar _ -> true
| EVar x ->
assert (x `Set.mem` (freevars e));
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2 | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.parse_bounded_bcvli | val parse_bounded_bcvli
(min: nat)
(max: nat { min <= max })
: Tot (parser (parse_bounded_bcvli_kind min max) (bounded_int32 min max)) | val parse_bounded_bcvli
(min: nat)
(max: nat { min <= max })
: Tot (parser (parse_bounded_bcvli_kind min max) (bounded_int32 min max)) | let parse_bounded_bcvli
min max
= parse_bounded_bcvli_kind_correct min max;
strengthen (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max) | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 78,
"end_line": 132,
"start_col": 0,
"start_line": 129
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options
let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload
let parse_bcvli_eq
b
= and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b'
let bare_serialize_bcvli (x: U32.t) : GTot bytes =
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
Seq.append
(serialize (serialize_bounded_integer_le 1) c1)
(
if U32.v c1 <= 252 then Seq.empty else
if U32.v c1 = 253 then serialize (serialize_bounded_integer_le 2) x else
serialize (serialize_bounded_integer_le 4) x
)
#push-options "--z3rlimit 32"
let bare_serialize_bcvli_correct : squash
(serializer_correct parse_bcvli bare_serialize_bcvli)
= let prf (x: U32.t) : Lemma
(let y = bare_serialize_bcvli x in
parse parse_bcvli y == Some (x, Seq.length y))
= let y = bare_serialize_bcvli x in
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
let sc1 = serialize (serialize_bounded_integer_le 1) c1 in
parse_strong_prefix (parse_bounded_integer_le 1) sc1 y;
let y' = Seq.slice y (Seq.length sc1) (Seq.length y) in
parse_bcvli_eq y;
if U32.v c1 <= 252 then begin
assert (y `Seq.equal` sc1)
end else if U32.v c1 = 253 then begin
assert (U32.v x <= 65535);
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 2) x)
end else begin
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 4) x)
end
in
Classical.forall_intro (Classical.move_requires prf)
#pop-options
let serialize_bcvli : serializer parse_bcvli = bare_serialize_bcvli
let serialize_bcvli_eq x = ()
let parse_bounded_bcvli'
(min: nat)
(max: nat { min <= max })
: Tot (parser (parse_filter_kind parse_bcvli_kind) (bounded_int32 min max))
= parse_filter parse_bcvli (in_bounds min max)
let parse_bounded_bcvli_size_correct
(min: nat)
(max: nat { min <= max })
: Lemma
(parse_bounded_bcvli_size min `parses_at_least` parse_bounded_bcvli' min max /\
parse_bounded_bcvli_size max `parses_at_most` parse_bounded_bcvli' min max)
= Classical.forall_intro (parse_filter_eq parse_bcvli (in_bounds min max));
Classical.forall_intro parse_bcvli_eq;
parser_kind_prop_equiv (parse_bounded_integer_kind 1) (parse_bounded_integer_le 1);
parser_kind_prop_equiv (parse_bounded_integer_kind 2) (parse_bounded_integer_le 2);
parser_kind_prop_equiv (parse_bounded_integer_kind 4) (parse_bounded_integer_le 4)
let parse_bounded_bcvli_kind_correct
(min: nat)
(max: nat { min <= max })
: Lemma
(parser_kind_prop (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max))
= parse_bounded_bcvli_size_correct min max;
parser_kind_prop_equiv (parse_filter_kind parse_bcvli_kind) (parse_bounded_bcvli' min max);
parser_kind_prop_equiv (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | min: Prims.nat -> max: Prims.nat{min <= max}
-> LowParse.Spec.Base.parser (LowParse.Spec.BCVLI.parse_bounded_bcvli_kind min max)
(LowParse.Spec.BoundedInt.bounded_int32 min max) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.Spec.Base.strengthen",
"LowParse.Spec.BCVLI.parse_bounded_bcvli_kind",
"LowParse.Spec.BoundedInt.bounded_int32",
"LowParse.Spec.BCVLI.parse_bounded_bcvli'",
"Prims.unit",
"LowParse.Spec.BCVLI.parse_bounded_bcvli_kind_correct",
"LowParse.Spec.Base.parser"
] | [] | false | false | false | false | false | let parse_bounded_bcvli min max =
| parse_bounded_bcvli_kind_correct min max;
strengthen (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max) | false |
STLC.Core.fst | STLC.Core.elab_open_commute | val elab_open_commute (e: stlc_exp) (x: var)
: Lemma (RT.open_term (elab_exp e) x == elab_exp (open_exp e x))
[SMTPat (RT.open_term (elab_exp e) x)] | val elab_open_commute (e: stlc_exp) (x: var)
: Lemma (RT.open_term (elab_exp e) x == elab_exp (open_exp e x))
[SMTPat (RT.open_term (elab_exp e) x)] | let elab_open_commute (e:stlc_exp) (x:var)
: Lemma (RT.open_term (elab_exp e) x == elab_exp (open_exp e x))
[SMTPat (RT.open_term (elab_exp e) x)]
= elab_open_commute' e x 0;
RT.open_term_spec (elab_exp e) x | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 411,
"start_col": 0,
"start_line": 407
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v
let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x
let stlc_types_are_closed3 (ty:stlc_ty) (x:R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.open_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.DT 0 (RT.var_as_term x) ];
RT.open_term_spec (elab_ty ty) x
let rec elab_ty_freevars (ty:stlc_ty)
: Lemma (RT.freevars (elab_ty ty) `Set.equal` Set.empty)
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
elab_ty_freevars t1;
elab_ty_freevars t2
let rec elab_open_commute' (e:stlc_exp) (x:var) (n:nat)
: Lemma (ensures
RT.subst_term (elab_exp e) (RT.open_with_var x n) ==
elab_exp (open_exp' e x n))
(decreases e)
= match e with
| EUnit -> ()
| EBVar _ -> ()
| EVar _ -> ()
| EApp e1 e2 ->
elab_open_commute' e1 x n;
elab_open_commute' e2 x n
| ELam t e ->
calc (==) {
elab_exp (open_exp' (ELam t e) x n);
(==) {}
elab_exp (ELam t (open_exp' e x (n + 1)));
(==) { }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp (open_exp' e x (n + 1)))));
(==) { elab_open_commute' e x (n + 1) }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(RT.subst_term (elab_exp e) RT.(open_with_var x (n + 1)))));
(==) { stlc_types_are_closed_core t (RT.open_with_var x n) }
RT.subst_term
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp e)))
RT.(open_with_var x n);
} | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: STLC.Core.stlc_exp -> x: STLC.Core.var
-> FStar.Pervasives.Lemma
(ensures
FStar.Reflection.Typing.open_term (STLC.Core.elab_exp e) x ==
STLC.Core.elab_exp (STLC.Core.open_exp e x))
[SMTPat (FStar.Reflection.Typing.open_term (STLC.Core.elab_exp e) x)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"STLC.Core.stlc_exp",
"STLC.Core.var",
"FStar.Reflection.Typing.open_term_spec",
"STLC.Core.elab_exp",
"Prims.unit",
"STLC.Core.elab_open_commute'",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.Stubs.Reflection.Types.term",
"FStar.Reflection.Typing.open_term",
"STLC.Core.open_exp",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [] | true | false | true | false | false | let elab_open_commute (e: stlc_exp) (x: var)
: Lemma (RT.open_term (elab_exp e) x == elab_exp (open_exp e x))
[SMTPat (RT.open_term (elab_exp e) x)] =
| elab_open_commute' e x 0;
RT.open_term_spec (elab_exp e) x | false |
STLC.Core.fst | STLC.Core.elab_ty | val elab_ty (t: stlc_ty) : R.term | val elab_ty (t: stlc_ty) : R.term | let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2))) | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 295,
"start_col": 0,
"start_line": 281
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | t: STLC.Core.stlc_ty -> FStar.Stubs.Reflection.Types.term | Prims.Tot | [
"total"
] | [] | [
"STLC.Core.stlc_ty",
"FStar.Stubs.Reflection.V2.Builtins.pack_ln",
"FStar.Stubs.Reflection.V2.Data.Tv_FVar",
"FStar.Stubs.Reflection.V2.Builtins.pack_fv",
"FStar.Reflection.Const.unit_lid",
"FStar.Stubs.Reflection.V2.Data.Tv_Arrow",
"FStar.Reflection.Typing.mk_simple_binder",
"FStar.Reflection.Typing.pp_name_default",
"FStar.Stubs.Reflection.V2.Builtins.pack_comp",
"FStar.Stubs.Reflection.V2.Data.C_Total",
"FStar.Stubs.Reflection.Types.term",
"STLC.Core.elab_ty"
] | [
"recursion"
] | false | false | false | true | false | let rec elab_ty (t: stlc_ty) : R.term =
| let open R in
match t with
| TUnit -> R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln (R.Tv_Arrow (RT.mk_simple_binder RT.pp_name_default t1) (R.pack_comp (C_Total t2))) | false |
STLC.Core.fst | STLC.Core.stlc_types_are_closed3 | val stlc_types_are_closed3 (ty: stlc_ty) (x: R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty) [SMTPat (RT.open_term (elab_ty ty) x)] | val stlc_types_are_closed3 (ty: stlc_ty) (x: R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty) [SMTPat (RT.open_term (elab_ty ty) x)] | let stlc_types_are_closed3 (ty:stlc_ty) (x:R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.open_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.DT 0 (RT.var_as_term x) ];
RT.open_term_spec (elab_ty ty) x | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 369,
"start_col": 0,
"start_line": 365
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v
let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | ty: STLC.Core.stlc_ty -> x: FStar.Stubs.Reflection.V2.Data.var
-> FStar.Pervasives.Lemma
(ensures FStar.Reflection.Typing.open_term (STLC.Core.elab_ty ty) x == STLC.Core.elab_ty ty)
[SMTPat (FStar.Reflection.Typing.open_term (STLC.Core.elab_ty ty) x)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"STLC.Core.stlc_ty",
"FStar.Stubs.Reflection.V2.Data.var",
"FStar.Reflection.Typing.open_term_spec",
"STLC.Core.elab_ty",
"Prims.unit",
"STLC.Core.stlc_types_are_closed_core",
"Prims.Cons",
"FStar.Reflection.Typing.subst_elt",
"FStar.Reflection.Typing.DT",
"FStar.Reflection.Typing.var_as_term",
"Prims.Nil",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.Stubs.Reflection.Types.term",
"FStar.Reflection.Typing.open_term",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat"
] | [] | true | false | true | false | false | let stlc_types_are_closed3 (ty: stlc_ty) (x: R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty) [SMTPat (RT.open_term (elab_ty ty) x)] =
| stlc_types_are_closed_core ty [RT.DT 0 (RT.var_as_term x)];
RT.open_term_spec (elab_ty ty) x | false |
EverParse3d.Prelude.fst | EverParse3d.Prelude.validate_nlist_total_constant_size_mod_ok | val validate_nlist_total_constant_size_mod_ok
(n: U32.t)
(#wk: _)
(#k: parser_kind true wk)
(#t: Type)
(p: parser k t)
: Pure (validator_no_read (parse_nlist n p))
(requires
(let open LP in
k.parser_kind_subkind == Some ParserStrong /\ k.parser_kind_high == Some k.parser_kind_low /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low < 4294967296 /\
U32.v n % k.LP.parser_kind_low == 0))
(ensures (fun _ -> True)) | val validate_nlist_total_constant_size_mod_ok
(n: U32.t)
(#wk: _)
(#k: parser_kind true wk)
(#t: Type)
(p: parser k t)
: Pure (validator_no_read (parse_nlist n p))
(requires
(let open LP in
k.parser_kind_subkind == Some ParserStrong /\ k.parser_kind_high == Some k.parser_kind_low /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low < 4294967296 /\
U32.v n % k.LP.parser_kind_low == 0))
(ensures (fun _ -> True)) | let validate_nlist_total_constant_size_mod_ok (n:U32.t) #wk (#k:parser_kind true wk) (#t: Type) (p:parser k t)
: Pure (validator_no_read (parse_nlist n p))
(requires (
let open LP in
k.parser_kind_subkind == Some ParserStrong /\
k.parser_kind_high == Some k.parser_kind_low /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
k.parser_kind_low < 4294967296 /\
U32.v n % k.LP.parser_kind_low == 0
))
(ensures (fun _ -> True))
=
(fun #rrel #rel sl len pos ->
let h = FStar.HyperStack.ST.get () in
[@inline_let]
let _ =
parse_nlist_total_fixed_size_kind_correct n p;
LPL.valid_facts (parse_nlist n p) h sl (LPL.uint64_to_uint32 pos);
LPL.valid_facts (LP.strengthen (LP.total_constant_size_parser_kind (U32.v n)) (parse_nlist n p)) h sl (LPL.uint64_to_uint32 pos)
in
LPL.validate_total_constant_size_no_read (LP.strengthen (LP.total_constant_size_parser_kind (U32.v n)) (parse_nlist n p)) (FStar.Int.Cast.uint32_to_uint64 n) () sl len pos
) | {
"file_name": "src/3d/prelude/EverParse3d.Prelude.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 7,
"end_line": 243,
"start_col": 0,
"start_line": 222
} | (*
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 EverParse3d.Prelude
friend EverParse3d.Kinds
module BF = LowParse.BitFields
module LP = LowParse.Spec.Base
module LPC = LowParse.Spec.Combinators
module LPL = LowParse.Low.Base
module LPLC = LowParse.Low.Combinators
module U16 = FStar.UInt16
module U32 = FStar.UInt32
module U64 = FStar.UInt64
////////////////////////////////////////////////////////////////////////////////
// Parsers
////////////////////////////////////////////////////////////////////////////////
let parser k t = LP.parser k t
let is_weaker_than #nz1 #wk1 (k:parser_kind nz1 wk1)
#nz2 #wk2 (k':parser_kind nz2 wk2) = k `LP.is_weaker_than` k'
let is_weaker_than_refl #nz #wk (k:parser_kind nz wk)
: Lemma (ensures (is_weaker_than k k))
[SMTPat (is_weaker_than k k)]
= ()
let is_weaker_than_glb #nz1 #wk1 (k1:parser_kind nz1 wk1)
#nz2 #wk2 (k2:parser_kind nz2 wk2)
: Lemma (is_weaker_than (glb k1 k2) k1 /\
is_weaker_than (glb k1 k2) k2)
[SMTPatOr
[[SMTPat (is_weaker_than (glb k1 k2) k1)];
[SMTPat (is_weaker_than (glb k1 k2) k2)]]]
= ()
/// Parser: return
inline_for_extraction noextract
let parse_ret #t (v:t)
: Tot (parser ret_kind t)
= LPC.parse_ret #t v
/// Parser: bind
inline_for_extraction noextract
let parse_dep_pair p1 p2
= LPC.parse_dtuple2 p1 p2
/// Parser: sequencing
inline_for_extraction noextract
let parse_pair p1 p2
= LPC.nondep_then p1 p2
/// Parser: map
let injective_map a b = (a -> Tot b) //{LPC.synth_injective f}
inline_for_extraction noextract
let parse_filter p f
= LPC.parse_filter p f
/// Parser: weakening kinds
inline_for_extraction noextract
let parse_weaken #nz #wk (#k:parser_kind nz wk) #t (p:parser k t)
#nz' #wk' (k':parser_kind nz' wk' {k' `is_weaker_than` k})
: Tot (parser k' t)
= LP.weaken k' p
/// Parser: weakening kinds
inline_for_extraction noextract
let parse_weaken_left #nz #wk #k p k'
= LP.weaken (glb k' k) p
/// Parser: weakening kinds
inline_for_extraction noextract
let parse_weaken_right #nz #wk #k p k'
= LP.weaken (glb k k') p
/// Parser: unreachable, for default cases of exhaustive pattern matching
inline_for_extraction noextract
let parse_impos ()
: parser impos_kind False
= let p : LP.bare_parser False = fun b -> None in
LP.parser_kind_prop_equiv impos_kind p;
p
let parse_ite e p1 p2
= if e then p1 () else p2 ()
let nlist (n:U32.t) (t:Type) = list t
inline_for_extraction noextract
let parse_nlist n #wk #k #t p
= let open LowParse.Spec.FLData in
let open LowParse.Spec.List in
parse_weaken
#false #WeakKindStrongPrefix #(parse_fldata_kind (U32.v n) parse_list_kind) #(list t)
(LowParse.Spec.FLData.parse_fldata (LowParse.Spec.List.parse_list p) (U32.v n))
#false kind_nlist
let all_bytes = Seq.seq LP.byte
let parse_all_bytes'
: LP.bare_parser all_bytes
= fun input -> Some (input, (Seq.length input <: LP.consumed_length input))
let parse_all_bytes =
LP.parser_kind_prop_equiv kind_all_bytes parse_all_bytes';
parse_all_bytes'
////////////////////////////////////////////////////////////////////////////////
module B32 = FStar.Bytes
let t_at_most (n:U32.t) (t:Type) = t & all_bytes
inline_for_extraction noextract
let parse_t_at_most n #nz #wk #k #t p
= let open LowParse.Spec.FLData in
let open LowParse.Spec.List in
parse_weaken
#false
#WeakKindStrongPrefix
(LowParse.Spec.FLData.parse_fldata
(LPC.nondep_then p parse_all_bytes)
(U32.v n))
#false
kind_t_at_most
////////////////////////////////////////////////////////////////////////////////
let t_exact (n:U32.t) (t:Type) = t
inline_for_extraction noextract
let parse_t_exact n #nz #wk #k #t p
= let open LowParse.Spec.FLData in
let open LowParse.Spec.List in
parse_weaken
#false
#WeakKindStrongPrefix
(LowParse.Spec.FLData.parse_fldata
p
(U32.v n))
#false
kind_t_exact
////////////////////////////////////////////////////////////////////////////////
// Readers
////////////////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
let reader p = LPLC.leaf_reader p
inline_for_extraction noextract
let read_filter p32 f
= LPLC.read_filter p32 f
let read_impos : reader (parse_impos()) =
fun #rrel #rel sl pos ->
let h = FStar.HyperStack.ST.get() in
assert (LPLC.valid (parse_impos()) h sl pos);
LowParse.Low.Base.Spec.valid_equiv (parse_impos()) h sl pos;
false_elim ()
// ////////////////////////////////////////////////////////////////////////////////
// // Validators
// ////////////////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
let validator #nz #wk (#k:parser_kind nz wk) (#t:Type) (p:parser k t)
: Type
= LPL.validator #k #t p
inline_for_extraction noextract
let validator_no_read #nz #wk (#k:parser_kind nz wk) (#t:Type) (p:parser k t)
: Type
= LPL.validator_no_read #k #t p
let parse_nlist_total_fixed_size_aux
(n:U32.t) (#wk: _) (#k:parser_kind true wk) #t (p:parser k t)
(x: LP.bytes)
: Lemma
(requires (
let open LP in
k.parser_kind_subkind == Some ParserStrong /\
k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\
Seq.length x >= U32.v n
))
(ensures (
Some? (LP.parse (parse_nlist n p) x)
))
= let x' = Seq.slice x 0 (U32.v n) in
let cnt = (U32.v n / k.LP.parser_kind_low) in
FStar.Math.Lemmas.lemma_div_exact (U32.v n) k.LP.parser_kind_low;
FStar.Math.Lemmas.nat_over_pos_is_nat (U32.v n) k.LP.parser_kind_low;
LowParse.Spec.List.parse_list_total_constant_size p cnt x';
LP.parser_kind_prop_equiv LowParse.Spec.List.parse_list_kind (LowParse.Spec.List.parse_list p)
let parse_nlist_total_fixed_size_kind_correct
(n:U32.t) (#wk: _) (#k:parser_kind true wk) #t (p:parser k t)
: Lemma
(requires (
let open LP in
k.parser_kind_subkind == Some ParserStrong /\
k.parser_kind_high == Some k.parser_kind_low /\
U32.v n % k.parser_kind_low == 0 /\
k.parser_kind_metadata == Some ParserKindMetadataTotal
))
(ensures (
LP.parser_kind_prop (LP.total_constant_size_parser_kind (U32.v n)) (parse_nlist n p)
))
= LP.parser_kind_prop_equiv (LowParse.Spec.FLData.parse_fldata_kind (U32.v n) LowParse.Spec.List.parse_list_kind) (parse_nlist n p);
LP.parser_kind_prop_equiv (LP.total_constant_size_parser_kind (U32.v n)) (parse_nlist n p);
Classical.forall_intro (Classical.move_requires (parse_nlist_total_fixed_size_aux n p)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.ListUpTo.fsti.checked",
"LowParse.Spec.List.fsti.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"LowParse.Spec.Base.fsti.checked",
"LowParse.Low.Int.fsti.checked",
"LowParse.Low.Combinators.fsti.checked",
"LowParse.Low.BoundedInt.fsti.checked",
"LowParse.Low.Base.Spec.fsti.checked",
"LowParse.Low.Base.fst.checked",
"LowParse.BitFields.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Bytes.fsti.checked",
"EverParse3d.Kinds.fst.checked"
],
"interface_file": true,
"source_file": "EverParse3d.Prelude.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Bytes",
"short_module": "B32"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Combinators",
"short_module": "LPLC"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Base",
"short_module": "LPL"
},
{
"abbrev": true,
"full_module": "LowParse.Spec.Combinators",
"short_module": "LPC"
},
{
"abbrev": true,
"full_module": "LowParse.Spec.Base",
"short_module": "LP"
},
{
"abbrev": true,
"full_module": "LowParse.BitFields",
"short_module": "BF"
},
{
"abbrev": true,
"full_module": "FStar.Int.Cast",
"short_module": "C"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": false,
"full_module": "FStar.Range",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.Kinds",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.Prelude.StaticHeader",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d",
"short_module": null
},
{
"abbrev": 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": 2,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [
"smt.qi.eager_threshold=10"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: FStar.UInt32.t -> p: EverParse3d.Prelude.parser k t
-> Prims.Pure (EverParse3d.Prelude.validator_no_read (EverParse3d.Prelude.parse_nlist n p)) | Prims.Pure | [] | [] | [
"FStar.UInt32.t",
"EverParse3d.Kinds.weak_kind",
"EverParse3d.Kinds.parser_kind",
"EverParse3d.Prelude.parser",
"LowParse.Slice.srel",
"LowParse.Bytes.byte",
"FStar.Ghost.erased",
"LowParse.Slice.slice",
"Prims.eq2",
"LowParse.Slice.__proj__Mkslice__item__len",
"FStar.Ghost.reveal",
"FStar.UInt64.t",
"LowParse.Low.Base.validate_total_constant_size_no_read",
"LowParse.Spec.Base.total_constant_size_parser_kind",
"FStar.UInt32.v",
"EverParse3d.Prelude.nlist",
"LowParse.Spec.Base.strengthen",
"EverParse3d.Prelude.parse_nlist",
"FStar.Int.Cast.uint32_to_uint64",
"Prims.unit",
"LowParse.Low.Base.Spec.valid_facts",
"LowParse.Low.ErrorCode.uint64_to_uint32",
"EverParse3d.Kinds.kind_nlist",
"EverParse3d.Prelude.parse_nlist_total_fixed_size_kind_correct",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"EverParse3d.Prelude.validator_no_read",
"EverParse3d.Kinds.WeakKindStrongPrefix",
"Prims.l_and",
"FStar.Pervasives.Native.option",
"LowParse.Spec.Base.parser_subkind",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_subkind",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.ParserStrong",
"Prims.nat",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"LowParse.Spec.Base.parser_kind_metadata_some",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata",
"LowParse.Spec.Base.ParserKindMetadataTotal",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.int",
"Prims.op_Modulus",
"Prims.l_True"
] | [] | false | false | false | false | false | let validate_nlist_total_constant_size_mod_ok
(n: U32.t)
#wk
(#k: parser_kind true wk)
(#t: Type)
(p: parser k t)
: Pure (validator_no_read (parse_nlist n p))
(requires
(let open LP in
k.parser_kind_subkind == Some ParserStrong /\ k.parser_kind_high == Some k.parser_kind_low /\
k.parser_kind_metadata == Some ParserKindMetadataTotal /\ k.parser_kind_low < 4294967296 /\
U32.v n % k.LP.parser_kind_low == 0))
(ensures (fun _ -> True)) =
| (fun #rrel #rel sl len pos ->
let h = FStar.HyperStack.ST.get () in
[@@ inline_let ]let _ =
parse_nlist_total_fixed_size_kind_correct n p;
LPL.valid_facts (parse_nlist n p) h sl (LPL.uint64_to_uint32 pos);
LPL.valid_facts (LP.strengthen (LP.total_constant_size_parser_kind (U32.v n))
(parse_nlist n p))
h
sl
(LPL.uint64_to_uint32 pos)
in
LPL.validate_total_constant_size_no_read (LP.strengthen (LP.total_constant_size_parser_kind (U32.v
n))
(parse_nlist n p))
(FStar.Int.Cast.uint32_to_uint64 n)
()
sl
len
pos) | false |
STLC.Core.fst | STLC.Core.soundness_lemma | val soundness_lemma (sg: stlc_env) (se: stlc_exp) (st: stlc_ty) (g: RT.fstar_top_env)
: Lemma (requires stlc_typing sg se st)
(ensures RT.tot_typing (extend_env_l g sg) (elab_exp se) (elab_ty st)) | val soundness_lemma (sg: stlc_env) (se: stlc_exp) (st: stlc_ty) (g: RT.fstar_top_env)
: Lemma (requires stlc_typing sg se st)
(ensures RT.tot_typing (extend_env_l g sg) (elab_exp se) (elab_ty st)) | let soundness_lemma (sg:stlc_env)
(se:stlc_exp)
(st:stlc_ty)
(g:RT.fstar_top_env)
: Lemma
(requires stlc_typing sg se st)
(ensures RT.tot_typing (extend_env_l g sg)
(elab_exp se)
(elab_ty st))
= FStar.Squash.bind_squash
#(stlc_typing sg se st)
()
(fun dd -> FStar.Squash.return_squash (soundness dd g)) | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 61,
"end_line": 537,
"start_col": 0,
"start_line": 525
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v
let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x
let stlc_types_are_closed3 (ty:stlc_ty) (x:R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.open_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.DT 0 (RT.var_as_term x) ];
RT.open_term_spec (elab_ty ty) x
let rec elab_ty_freevars (ty:stlc_ty)
: Lemma (RT.freevars (elab_ty ty) `Set.equal` Set.empty)
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
elab_ty_freevars t1;
elab_ty_freevars t2
let rec elab_open_commute' (e:stlc_exp) (x:var) (n:nat)
: Lemma (ensures
RT.subst_term (elab_exp e) (RT.open_with_var x n) ==
elab_exp (open_exp' e x n))
(decreases e)
= match e with
| EUnit -> ()
| EBVar _ -> ()
| EVar _ -> ()
| EApp e1 e2 ->
elab_open_commute' e1 x n;
elab_open_commute' e2 x n
| ELam t e ->
calc (==) {
elab_exp (open_exp' (ELam t e) x n);
(==) {}
elab_exp (ELam t (open_exp' e x (n + 1)));
(==) { }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp (open_exp' e x (n + 1)))));
(==) { elab_open_commute' e x (n + 1) }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(RT.subst_term (elab_exp e) RT.(open_with_var x (n + 1)))));
(==) { stlc_types_are_closed_core t (RT.open_with_var x n) }
RT.subst_term
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp e)))
RT.(open_with_var x n);
}
let elab_open_commute (e:stlc_exp) (x:var)
: Lemma (RT.open_term (elab_exp e) x == elab_exp (open_exp e x))
[SMTPat (RT.open_term (elab_exp e) x)]
= elab_open_commute' e x 0;
RT.open_term_spec (elab_exp e) x
let rec extend_env_l_lookup_fvar (g:R.env) (sg:stlc_env) (fv:R.fv)
: Lemma
(ensures
RT.lookup_fvar (extend_env_l g sg) fv ==
RT.lookup_fvar g fv)
[SMTPat (RT.lookup_fvar (extend_env_l g sg) fv)]
= match sg with
| [] -> ()
| hd::tl -> extend_env_l_lookup_fvar g tl fv
let rec elab_ty_soundness (g:RT.fstar_top_env)
(sg:stlc_env)
(t:stlc_ty)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_ty t) (RT.tm_type RT.u_zero))
(decreases t)
= match t with
| TUnit ->
RT.T_FVar _ RT.unit_fv
| TArrow t1 t2 ->
let t1_ok = elab_ty_soundness g sg t1 in
let x = fresh sg in
fresh_is_fresh sg;
elab_ty_freevars t2;
let t2_ok = elab_ty_soundness g ((x, t1)::sg) t2 in
let arr_max
: RT.tot_typing
(extend_env_l g sg)
(elab_ty t)
(RT.tm_type RT.(u_max u_zero u_zero))
= RT.T_Arrow _ x (elab_ty t1) (elab_ty t2)
_ _ RT.pp_name_default R.Q_Explicit T.E_Total _ _ t1_ok t2_ok
in
RT.simplify_umax arr_max
let rec elab_exp_freevars (e:stlc_exp)
: Lemma (freevars e `Set.equal` RT.freevars (elab_exp e))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam t e ->
elab_ty_freevars t;
elab_exp_freevars e
| EApp e1 e2 ->
elab_exp_freevars e1;
elab_exp_freevars e2
let rec soundness (#sg:stlc_env)
(#se:stlc_exp)
(#st:stlc_ty)
(dd:stlc_typing sg se st)
(g:RT.fstar_top_env)
: GTot (RT.tot_typing (extend_env_l g sg)
(elab_exp se)
(elab_ty st))
(decreases dd)
= match dd with
| T_Unit _ ->
RT.T_Const _ _ _ RT.CT_Unit
| T_Var _ x ->
RT.T_Var _ (R.pack_namedv (RT.make_namedv x))
| T_Lam _ t e t' x de ->
let de : RT.tot_typing (extend_env_l g ((x,t)::sg))
(elab_exp (open_exp e x))
(elab_ty t')
= soundness de g
in
let de : RT.tot_typing (RT.extend_env (extend_env_l g sg) x (elab_ty t))
(elab_exp (open_exp e x))
(elab_ty t')
= de
in
fresh_is_fresh sg;
elab_exp_freevars e;
let dd
= RT.T_Abs (extend_env_l g sg)
x
(elab_ty t)
(elab_exp e)
(T.E_Total, elab_ty t')
_
RT.pp_name_default
R.Q_Explicit
_
(elab_ty_soundness g sg t)
de
in
dd
| T_App _ e1 e2 t t' d1 d2 ->
let dt1
: RT.tot_typing (extend_env_l g sg)
(elab_exp e1)
(elab_ty (TArrow t t'))
= soundness d1 g
in
let dt2
: RT.tot_typing (extend_env_l g sg)
(elab_exp e2)
(elab_ty t)
= soundness d2 g
in
let dt :
RT.tot_typing (extend_env_l g sg)
(elab_exp (EApp e1 e2))
(RT.open_with (elab_ty t') (elab_exp e2))
= RT.T_App _ _ _ _ _ _ dt1 dt2
in
dt | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 |
sg: STLC.Core.stlc_env ->
se: STLC.Core.stlc_exp ->
st: STLC.Core.stlc_ty ->
g: FStar.Reflection.Typing.fstar_top_env
-> FStar.Pervasives.Lemma (requires STLC.Core.stlc_typing sg se st)
(ensures
FStar.Reflection.Typing.tot_typing (STLC.Core.extend_env_l g sg)
(STLC.Core.elab_exp se)
(STLC.Core.elab_ty st)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"STLC.Core.stlc_env",
"STLC.Core.stlc_exp",
"STLC.Core.stlc_ty",
"FStar.Reflection.Typing.fstar_top_env",
"FStar.Squash.bind_squash",
"STLC.Core.stlc_typing",
"FStar.Reflection.Typing.tot_typing",
"STLC.Core.extend_env_l",
"STLC.Core.elab_exp",
"STLC.Core.elab_ty",
"FStar.Squash.return_squash",
"STLC.Core.soundness",
"Prims.squash",
"Prims.unit",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let soundness_lemma (sg: stlc_env) (se: stlc_exp) (st: stlc_ty) (g: RT.fstar_top_env)
: Lemma (requires stlc_typing sg se st)
(ensures RT.tot_typing (extend_env_l g sg) (elab_exp se) (elab_ty st)) =
| FStar.Squash.bind_squash #(stlc_typing sg se st)
()
(fun dd -> FStar.Squash.return_squash (soundness dd g)) | false |
STLC.Core.fst | STLC.Core.fresh_not_mem | val fresh_not_mem (e: list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt) | val fresh_not_mem (e: list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt) | let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 38,
"end_line": 160,
"start_col": 0,
"start_line": 156
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: Prims.list (STLC.Core.var * 'a) -> elt: (STLC.Core.var * 'a)
-> FStar.Pervasives.Lemma
(ensures FStar.List.Tot.Base.memP elt e ==> STLC.Core.fresh e > FStar.Pervasives.Native.fst elt) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.var",
"STLC.Core.fresh_not_mem",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"FStar.List.Tot.Base.memP",
"Prims.b2t",
"Prims.op_GreaterThan",
"STLC.Core.fresh",
"FStar.Pervasives.Native.fst",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec fresh_not_mem (e: list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt) =
| match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt | false |
STLC.Core.fst | STLC.Core.elab_ty_soundness | val elab_ty_soundness (g: RT.fstar_top_env) (sg: stlc_env) (t: stlc_ty)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_ty t) (RT.tm_type RT.u_zero)) (decreases t) | val elab_ty_soundness (g: RT.fstar_top_env) (sg: stlc_env) (t: stlc_ty)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_ty t) (RT.tm_type RT.u_zero)) (decreases t) | let rec elab_ty_soundness (g:RT.fstar_top_env)
(sg:stlc_env)
(t:stlc_ty)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_ty t) (RT.tm_type RT.u_zero))
(decreases t)
= match t with
| TUnit ->
RT.T_FVar _ RT.unit_fv
| TArrow t1 t2 ->
let t1_ok = elab_ty_soundness g sg t1 in
let x = fresh sg in
fresh_is_fresh sg;
elab_ty_freevars t2;
let t2_ok = elab_ty_soundness g ((x, t1)::sg) t2 in
let arr_max
: RT.tot_typing
(extend_env_l g sg)
(elab_ty t)
(RT.tm_type RT.(u_max u_zero u_zero))
= RT.T_Arrow _ x (elab_ty t1) (elab_ty t2)
_ _ RT.pp_name_default R.Q_Explicit T.E_Total _ _ t1_ok t2_ok
in
RT.simplify_umax arr_max | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 446,
"start_col": 0,
"start_line": 423
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v
let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x
let stlc_types_are_closed3 (ty:stlc_ty) (x:R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.open_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.DT 0 (RT.var_as_term x) ];
RT.open_term_spec (elab_ty ty) x
let rec elab_ty_freevars (ty:stlc_ty)
: Lemma (RT.freevars (elab_ty ty) `Set.equal` Set.empty)
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
elab_ty_freevars t1;
elab_ty_freevars t2
let rec elab_open_commute' (e:stlc_exp) (x:var) (n:nat)
: Lemma (ensures
RT.subst_term (elab_exp e) (RT.open_with_var x n) ==
elab_exp (open_exp' e x n))
(decreases e)
= match e with
| EUnit -> ()
| EBVar _ -> ()
| EVar _ -> ()
| EApp e1 e2 ->
elab_open_commute' e1 x n;
elab_open_commute' e2 x n
| ELam t e ->
calc (==) {
elab_exp (open_exp' (ELam t e) x n);
(==) {}
elab_exp (ELam t (open_exp' e x (n + 1)));
(==) { }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp (open_exp' e x (n + 1)))));
(==) { elab_open_commute' e x (n + 1) }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(RT.subst_term (elab_exp e) RT.(open_with_var x (n + 1)))));
(==) { stlc_types_are_closed_core t (RT.open_with_var x n) }
RT.subst_term
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp e)))
RT.(open_with_var x n);
}
let elab_open_commute (e:stlc_exp) (x:var)
: Lemma (RT.open_term (elab_exp e) x == elab_exp (open_exp e x))
[SMTPat (RT.open_term (elab_exp e) x)]
= elab_open_commute' e x 0;
RT.open_term_spec (elab_exp e) x
let rec extend_env_l_lookup_fvar (g:R.env) (sg:stlc_env) (fv:R.fv)
: Lemma
(ensures
RT.lookup_fvar (extend_env_l g sg) fv ==
RT.lookup_fvar g fv)
[SMTPat (RT.lookup_fvar (extend_env_l g sg) fv)]
= match sg with
| [] -> ()
| hd::tl -> extend_env_l_lookup_fvar g tl fv | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | g: FStar.Reflection.Typing.fstar_top_env -> sg: STLC.Core.stlc_env -> t: STLC.Core.stlc_ty
-> Prims.GTot
(FStar.Reflection.Typing.tot_typing (STLC.Core.extend_env_l g sg)
(STLC.Core.elab_ty t)
(FStar.Reflection.Typing.tm_type FStar.Reflection.Typing.u_zero)) | Prims.GTot | [
"sometrivial",
""
] | [] | [
"FStar.Reflection.Typing.fstar_top_env",
"STLC.Core.stlc_env",
"STLC.Core.stlc_ty",
"FStar.Reflection.Typing.T_FVar",
"STLC.Core.extend_env_l",
"FStar.Reflection.Typing.unit_fv",
"FStar.Reflection.Typing.simplify_umax",
"STLC.Core.elab_ty",
"FStar.Reflection.Typing.u_zero",
"FStar.Reflection.Typing.tot_typing",
"FStar.Reflection.Typing.tm_type",
"FStar.Reflection.Typing.u_max",
"FStar.Reflection.Typing.T_Arrow",
"FStar.Reflection.Typing.pp_name_default",
"FStar.Stubs.Reflection.V2.Data.Q_Explicit",
"FStar.Stubs.TypeChecker.Core.E_Total",
"Prims.Cons",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.var",
"FStar.Pervasives.Native.Mktuple2",
"STLC.Core.elab_ty_soundness",
"Prims.unit",
"STLC.Core.elab_ty_freevars",
"STLC.Core.fresh_is_fresh",
"STLC.Core.fresh"
] | [
"recursion"
] | false | false | false | false | false | let rec elab_ty_soundness (g: RT.fstar_top_env) (sg: stlc_env) (t: stlc_ty)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_ty t) (RT.tm_type RT.u_zero)) (decreases t) =
| match t with
| TUnit -> RT.T_FVar _ RT.unit_fv
| TArrow t1 t2 ->
let t1_ok = elab_ty_soundness g sg t1 in
let x = fresh sg in
fresh_is_fresh sg;
elab_ty_freevars t2;
let t2_ok = elab_ty_soundness g ((x, t1) :: sg) t2 in
let arr_max:RT.tot_typing (extend_env_l g sg) (elab_ty t) (RT.tm_type RT.(u_max u_zero u_zero)) =
RT.T_Arrow _
x
(elab_ty t1)
(elab_ty t2)
_
_
RT.pp_name_default
R.Q_Explicit
T.E_Total
_
_
t1_ok
t2_ok
in
RT.simplify_umax arr_max | false |
STLC.Core.fst | STLC.Core.extend_env_l_lookup_bvar | val extend_env_l_lookup_bvar (g: R.env) (sg: stlc_env) (x: var)
: Lemma (requires (forall x. RT.lookup_bvar g x == None))
(ensures
(match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)] | val extend_env_l_lookup_bvar (g: R.env) (sg: stlc_env) (x: var)
: Lemma (requires (forall x. RT.lookup_bvar g x == None))
(ensures
(match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)] | let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 49,
"end_line": 336,
"start_col": 0,
"start_line": 325
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | g: FStar.Stubs.Reflection.Types.env -> sg: STLC.Core.stlc_env -> x: STLC.Core.var
-> FStar.Pervasives.Lemma
(requires
forall (x: Prims.int).
FStar.Reflection.Typing.lookup_bvar g x == FStar.Pervasives.Native.None)
(ensures
((match STLC.Core.lookup sg x with
| FStar.Pervasives.Native.Some #_ t ->
FStar.Reflection.Typing.lookup_bvar (STLC.Core.extend_env_l g sg) x ==
FStar.Pervasives.Native.Some (STLC.Core.elab_ty t)
| FStar.Pervasives.Native.None #_ ->
FStar.Reflection.Typing.lookup_bvar (STLC.Core.extend_env_l g sg) x ==
FStar.Pervasives.Native.None)
<:
Type0))
(decreases sg)
[SMTPat (FStar.Reflection.Typing.lookup_bvar (STLC.Core.extend_env_l g sg) x)] | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"FStar.Stubs.Reflection.Types.env",
"STLC.Core.stlc_env",
"STLC.Core.var",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.stlc_ty",
"Prims.list",
"STLC.Core.extend_env_l_lookup_bvar",
"Prims.unit",
"Prims.l_Forall",
"Prims.int",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Stubs.Reflection.Types.term",
"FStar.Reflection.Typing.lookup_bvar",
"FStar.Pervasives.Native.None",
"Prims.squash",
"STLC.Core.lookup",
"STLC.Core.extend_env_l",
"FStar.Pervasives.Native.Some",
"STLC.Core.elab_ty",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [
"recursion"
] | false | false | true | false | false | let rec extend_env_l_lookup_bvar (g: R.env) (sg: stlc_env) (x: var)
: Lemma (requires (forall x. RT.lookup_bvar g x == None))
(ensures
(match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)] =
| match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x | false |
STLC.Core.fst | STLC.Core.extend_env_l_lookup_fvar | val extend_env_l_lookup_fvar (g: R.env) (sg: stlc_env) (fv: R.fv)
: Lemma (ensures RT.lookup_fvar (extend_env_l g sg) fv == RT.lookup_fvar g fv)
[SMTPat (RT.lookup_fvar (extend_env_l g sg) fv)] | val extend_env_l_lookup_fvar (g: R.env) (sg: stlc_env) (fv: R.fv)
: Lemma (ensures RT.lookup_fvar (extend_env_l g sg) fv == RT.lookup_fvar g fv)
[SMTPat (RT.lookup_fvar (extend_env_l g sg) fv)] | let rec extend_env_l_lookup_fvar (g:R.env) (sg:stlc_env) (fv:R.fv)
: Lemma
(ensures
RT.lookup_fvar (extend_env_l g sg) fv ==
RT.lookup_fvar g fv)
[SMTPat (RT.lookup_fvar (extend_env_l g sg) fv)]
= match sg with
| [] -> ()
| hd::tl -> extend_env_l_lookup_fvar g tl fv | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 48,
"end_line": 421,
"start_col": 0,
"start_line": 413
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v
let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x
let stlc_types_are_closed3 (ty:stlc_ty) (x:R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.open_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.DT 0 (RT.var_as_term x) ];
RT.open_term_spec (elab_ty ty) x
let rec elab_ty_freevars (ty:stlc_ty)
: Lemma (RT.freevars (elab_ty ty) `Set.equal` Set.empty)
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
elab_ty_freevars t1;
elab_ty_freevars t2
let rec elab_open_commute' (e:stlc_exp) (x:var) (n:nat)
: Lemma (ensures
RT.subst_term (elab_exp e) (RT.open_with_var x n) ==
elab_exp (open_exp' e x n))
(decreases e)
= match e with
| EUnit -> ()
| EBVar _ -> ()
| EVar _ -> ()
| EApp e1 e2 ->
elab_open_commute' e1 x n;
elab_open_commute' e2 x n
| ELam t e ->
calc (==) {
elab_exp (open_exp' (ELam t e) x n);
(==) {}
elab_exp (ELam t (open_exp' e x (n + 1)));
(==) { }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp (open_exp' e x (n + 1)))));
(==) { elab_open_commute' e x (n + 1) }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(RT.subst_term (elab_exp e) RT.(open_with_var x (n + 1)))));
(==) { stlc_types_are_closed_core t (RT.open_with_var x n) }
RT.subst_term
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp e)))
RT.(open_with_var x n);
}
let elab_open_commute (e:stlc_exp) (x:var)
: Lemma (RT.open_term (elab_exp e) x == elab_exp (open_exp e x))
[SMTPat (RT.open_term (elab_exp e) x)]
= elab_open_commute' e x 0;
RT.open_term_spec (elab_exp e) x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | g: FStar.Stubs.Reflection.Types.env -> sg: STLC.Core.stlc_env -> fv: FStar.Stubs.Reflection.Types.fv
-> FStar.Pervasives.Lemma
(ensures
FStar.Reflection.Typing.lookup_fvar (STLC.Core.extend_env_l g sg) fv ==
FStar.Reflection.Typing.lookup_fvar g fv)
[SMTPat (FStar.Reflection.Typing.lookup_fvar (STLC.Core.extend_env_l g sg) fv)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.Stubs.Reflection.Types.env",
"STLC.Core.stlc_env",
"FStar.Stubs.Reflection.Types.fv",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.var",
"STLC.Core.stlc_ty",
"Prims.list",
"STLC.Core.extend_env_l_lookup_fvar",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Stubs.Reflection.Types.term",
"FStar.Reflection.Typing.lookup_fvar",
"STLC.Core.extend_env_l",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [
"recursion"
] | false | false | true | false | false | let rec extend_env_l_lookup_fvar (g: R.env) (sg: stlc_env) (fv: R.fv)
: Lemma (ensures RT.lookup_fvar (extend_env_l g sg) fv == RT.lookup_fvar g fv)
[SMTPat (RT.lookup_fvar (extend_env_l g sg) fv)] =
| match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_fvar g tl fv | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.lbytes | val lbytes : len: Lib.IntTypes.size_t -> Type0 | let lbytes len = lbuffer uint8 len | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 34,
"end_line": 28,
"start_col": 0,
"start_line": 28
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16 | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | len: Lib.IntTypes.size_t -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Lib.IntTypes.size_t",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8"
] | [] | false | false | false | true | true | let lbytes len =
| lbuffer uint8 len | false |
|
STLC.Core.fst | STLC.Core.main | val main (nm: string) (src: stlc_exp) : RT.dsl_tac_t | val main (nm: string) (src: stlc_exp) : RT.dsl_tac_t | let main (nm:string) (src:stlc_exp) : RT.dsl_tac_t =
fun g ->
if ln src && closed src
then
let (| src_ty, d |) = check g [] src in
soundness_lemma [] src src_ty g;
[RT.mk_checked_let g nm (elab_exp src) (elab_ty src_ty)]
else T.fail "Not locally nameless" | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 546,
"start_col": 0,
"start_line": 539
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v
let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x
let stlc_types_are_closed3 (ty:stlc_ty) (x:R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.open_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.DT 0 (RT.var_as_term x) ];
RT.open_term_spec (elab_ty ty) x
let rec elab_ty_freevars (ty:stlc_ty)
: Lemma (RT.freevars (elab_ty ty) `Set.equal` Set.empty)
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
elab_ty_freevars t1;
elab_ty_freevars t2
let rec elab_open_commute' (e:stlc_exp) (x:var) (n:nat)
: Lemma (ensures
RT.subst_term (elab_exp e) (RT.open_with_var x n) ==
elab_exp (open_exp' e x n))
(decreases e)
= match e with
| EUnit -> ()
| EBVar _ -> ()
| EVar _ -> ()
| EApp e1 e2 ->
elab_open_commute' e1 x n;
elab_open_commute' e2 x n
| ELam t e ->
calc (==) {
elab_exp (open_exp' (ELam t e) x n);
(==) {}
elab_exp (ELam t (open_exp' e x (n + 1)));
(==) { }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp (open_exp' e x (n + 1)))));
(==) { elab_open_commute' e x (n + 1) }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(RT.subst_term (elab_exp e) RT.(open_with_var x (n + 1)))));
(==) { stlc_types_are_closed_core t (RT.open_with_var x n) }
RT.subst_term
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp e)))
RT.(open_with_var x n);
}
let elab_open_commute (e:stlc_exp) (x:var)
: Lemma (RT.open_term (elab_exp e) x == elab_exp (open_exp e x))
[SMTPat (RT.open_term (elab_exp e) x)]
= elab_open_commute' e x 0;
RT.open_term_spec (elab_exp e) x
let rec extend_env_l_lookup_fvar (g:R.env) (sg:stlc_env) (fv:R.fv)
: Lemma
(ensures
RT.lookup_fvar (extend_env_l g sg) fv ==
RT.lookup_fvar g fv)
[SMTPat (RT.lookup_fvar (extend_env_l g sg) fv)]
= match sg with
| [] -> ()
| hd::tl -> extend_env_l_lookup_fvar g tl fv
let rec elab_ty_soundness (g:RT.fstar_top_env)
(sg:stlc_env)
(t:stlc_ty)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_ty t) (RT.tm_type RT.u_zero))
(decreases t)
= match t with
| TUnit ->
RT.T_FVar _ RT.unit_fv
| TArrow t1 t2 ->
let t1_ok = elab_ty_soundness g sg t1 in
let x = fresh sg in
fresh_is_fresh sg;
elab_ty_freevars t2;
let t2_ok = elab_ty_soundness g ((x, t1)::sg) t2 in
let arr_max
: RT.tot_typing
(extend_env_l g sg)
(elab_ty t)
(RT.tm_type RT.(u_max u_zero u_zero))
= RT.T_Arrow _ x (elab_ty t1) (elab_ty t2)
_ _ RT.pp_name_default R.Q_Explicit T.E_Total _ _ t1_ok t2_ok
in
RT.simplify_umax arr_max
let rec elab_exp_freevars (e:stlc_exp)
: Lemma (freevars e `Set.equal` RT.freevars (elab_exp e))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam t e ->
elab_ty_freevars t;
elab_exp_freevars e
| EApp e1 e2 ->
elab_exp_freevars e1;
elab_exp_freevars e2
let rec soundness (#sg:stlc_env)
(#se:stlc_exp)
(#st:stlc_ty)
(dd:stlc_typing sg se st)
(g:RT.fstar_top_env)
: GTot (RT.tot_typing (extend_env_l g sg)
(elab_exp se)
(elab_ty st))
(decreases dd)
= match dd with
| T_Unit _ ->
RT.T_Const _ _ _ RT.CT_Unit
| T_Var _ x ->
RT.T_Var _ (R.pack_namedv (RT.make_namedv x))
| T_Lam _ t e t' x de ->
let de : RT.tot_typing (extend_env_l g ((x,t)::sg))
(elab_exp (open_exp e x))
(elab_ty t')
= soundness de g
in
let de : RT.tot_typing (RT.extend_env (extend_env_l g sg) x (elab_ty t))
(elab_exp (open_exp e x))
(elab_ty t')
= de
in
fresh_is_fresh sg;
elab_exp_freevars e;
let dd
= RT.T_Abs (extend_env_l g sg)
x
(elab_ty t)
(elab_exp e)
(T.E_Total, elab_ty t')
_
RT.pp_name_default
R.Q_Explicit
_
(elab_ty_soundness g sg t)
de
in
dd
| T_App _ e1 e2 t t' d1 d2 ->
let dt1
: RT.tot_typing (extend_env_l g sg)
(elab_exp e1)
(elab_ty (TArrow t t'))
= soundness d1 g
in
let dt2
: RT.tot_typing (extend_env_l g sg)
(elab_exp e2)
(elab_ty t)
= soundness d2 g
in
let dt :
RT.tot_typing (extend_env_l g sg)
(elab_exp (EApp e1 e2))
(RT.open_with (elab_ty t') (elab_exp e2))
= RT.T_App _ _ _ _ _ _ dt1 dt2
in
dt
let soundness_lemma (sg:stlc_env)
(se:stlc_exp)
(st:stlc_ty)
(g:RT.fstar_top_env)
: Lemma
(requires stlc_typing sg se st)
(ensures RT.tot_typing (extend_env_l g sg)
(elab_exp se)
(elab_ty st))
= FStar.Squash.bind_squash
#(stlc_typing sg se st)
()
(fun dd -> FStar.Squash.return_squash (soundness dd g)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | nm: Prims.string -> src: STLC.Core.stlc_exp -> FStar.Reflection.Typing.dsl_tac_t | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"STLC.Core.stlc_exp",
"FStar.Reflection.Typing.fstar_top_env",
"Prims.op_AmpAmp",
"STLC.Core.ln",
"STLC.Core.closed",
"STLC.Core.stlc_ty",
"STLC.Core.stlc_typing",
"Prims.Nil",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.var",
"Prims.Cons",
"FStar.Reflection.Typing.sigelt_for",
"FStar.Reflection.Typing.dsl_tac_result_t",
"FStar.Reflection.Typing.mk_checked_let",
"STLC.Core.elab_exp",
"STLC.Core.elab_ty",
"Prims.unit",
"STLC.Core.soundness_lemma",
"Prims.dtuple2",
"STLC.Core.check",
"Prims.bool",
"FStar.Tactics.V2.Derived.fail",
"FStar.Reflection.Typing.dsl_tac_t"
] | [] | false | false | false | true | false | let main (nm: string) (src: stlc_exp) : RT.dsl_tac_t =
| fun g ->
if ln src && closed src
then
let (| src_ty , d |) = check g [] src in
soundness_lemma [] src src_ty g;
[RT.mk_checked_let g nm (elab_exp src) (elab_ty src_ty)]
else T.fail "Not locally nameless" | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.elem | val elem : Type0 | let elem = uint16 | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 17,
"end_line": 25,
"start_col": 0,
"start_line": 25
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0" | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Lib.IntTypes.uint16"
] | [] | false | false | false | true | true | let elem =
| uint16 | false |
|
STLC.Core.fst | STLC.Core.elab_ty_freevars | val elab_ty_freevars (ty: stlc_ty) : Lemma ((RT.freevars (elab_ty ty)) `Set.equal` Set.empty) | val elab_ty_freevars (ty: stlc_ty) : Lemma ((RT.freevars (elab_ty ty)) `Set.equal` Set.empty) | let rec elab_ty_freevars (ty:stlc_ty)
: Lemma (RT.freevars (elab_ty ty) `Set.equal` Set.empty)
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
elab_ty_freevars t1;
elab_ty_freevars t2 | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 25,
"end_line": 377,
"start_col": 0,
"start_line": 371
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v
let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x
let stlc_types_are_closed3 (ty:stlc_ty) (x:R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.open_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.DT 0 (RT.var_as_term x) ];
RT.open_term_spec (elab_ty ty) x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | ty: STLC.Core.stlc_ty
-> FStar.Pervasives.Lemma
(ensures
FStar.Set.equal (FStar.Reflection.Typing.freevars (STLC.Core.elab_ty ty)) FStar.Set.empty) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"STLC.Core.stlc_ty",
"STLC.Core.elab_ty_freevars",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"FStar.Set.equal",
"FStar.Stubs.Reflection.V2.Data.var",
"FStar.Reflection.Typing.freevars",
"STLC.Core.elab_ty",
"FStar.Set.empty",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec elab_ty_freevars (ty: stlc_ty) : Lemma ((RT.freevars (elab_ty ty)) `Set.equal` Set.empty) =
| match ty with
| TUnit -> ()
| TArrow t1 t2 ->
elab_ty_freevars t1;
elab_ty_freevars t2 | false |
STLC.Core.fst | STLC.Core.soundness | val soundness
(#sg: stlc_env)
(#se: stlc_exp)
(#st: stlc_ty)
(dd: stlc_typing sg se st)
(g: RT.fstar_top_env)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_exp se) (elab_ty st)) (decreases dd) | val soundness
(#sg: stlc_env)
(#se: stlc_exp)
(#st: stlc_ty)
(dd: stlc_typing sg se st)
(g: RT.fstar_top_env)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_exp se) (elab_ty st)) (decreases dd) | let rec soundness (#sg:stlc_env)
(#se:stlc_exp)
(#st:stlc_ty)
(dd:stlc_typing sg se st)
(g:RT.fstar_top_env)
: GTot (RT.tot_typing (extend_env_l g sg)
(elab_exp se)
(elab_ty st))
(decreases dd)
= match dd with
| T_Unit _ ->
RT.T_Const _ _ _ RT.CT_Unit
| T_Var _ x ->
RT.T_Var _ (R.pack_namedv (RT.make_namedv x))
| T_Lam _ t e t' x de ->
let de : RT.tot_typing (extend_env_l g ((x,t)::sg))
(elab_exp (open_exp e x))
(elab_ty t')
= soundness de g
in
let de : RT.tot_typing (RT.extend_env (extend_env_l g sg) x (elab_ty t))
(elab_exp (open_exp e x))
(elab_ty t')
= de
in
fresh_is_fresh sg;
elab_exp_freevars e;
let dd
= RT.T_Abs (extend_env_l g sg)
x
(elab_ty t)
(elab_exp e)
(T.E_Total, elab_ty t')
_
RT.pp_name_default
R.Q_Explicit
_
(elab_ty_soundness g sg t)
de
in
dd
| T_App _ e1 e2 t t' d1 d2 ->
let dt1
: RT.tot_typing (extend_env_l g sg)
(elab_exp e1)
(elab_ty (TArrow t t'))
= soundness d1 g
in
let dt2
: RT.tot_typing (extend_env_l g sg)
(elab_exp e2)
(elab_ty t)
= soundness d2 g
in
let dt :
RT.tot_typing (extend_env_l g sg)
(elab_exp (EApp e1 e2))
(RT.open_with (elab_ty t') (elab_exp e2))
= RT.T_App _ _ _ _ _ _ dt1 dt2
in
dt | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 8,
"end_line": 523,
"start_col": 0,
"start_line": 461
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v
let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x
let stlc_types_are_closed3 (ty:stlc_ty) (x:R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.open_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.DT 0 (RT.var_as_term x) ];
RT.open_term_spec (elab_ty ty) x
let rec elab_ty_freevars (ty:stlc_ty)
: Lemma (RT.freevars (elab_ty ty) `Set.equal` Set.empty)
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
elab_ty_freevars t1;
elab_ty_freevars t2
let rec elab_open_commute' (e:stlc_exp) (x:var) (n:nat)
: Lemma (ensures
RT.subst_term (elab_exp e) (RT.open_with_var x n) ==
elab_exp (open_exp' e x n))
(decreases e)
= match e with
| EUnit -> ()
| EBVar _ -> ()
| EVar _ -> ()
| EApp e1 e2 ->
elab_open_commute' e1 x n;
elab_open_commute' e2 x n
| ELam t e ->
calc (==) {
elab_exp (open_exp' (ELam t e) x n);
(==) {}
elab_exp (ELam t (open_exp' e x (n + 1)));
(==) { }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp (open_exp' e x (n + 1)))));
(==) { elab_open_commute' e x (n + 1) }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(RT.subst_term (elab_exp e) RT.(open_with_var x (n + 1)))));
(==) { stlc_types_are_closed_core t (RT.open_with_var x n) }
RT.subst_term
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp e)))
RT.(open_with_var x n);
}
let elab_open_commute (e:stlc_exp) (x:var)
: Lemma (RT.open_term (elab_exp e) x == elab_exp (open_exp e x))
[SMTPat (RT.open_term (elab_exp e) x)]
= elab_open_commute' e x 0;
RT.open_term_spec (elab_exp e) x
let rec extend_env_l_lookup_fvar (g:R.env) (sg:stlc_env) (fv:R.fv)
: Lemma
(ensures
RT.lookup_fvar (extend_env_l g sg) fv ==
RT.lookup_fvar g fv)
[SMTPat (RT.lookup_fvar (extend_env_l g sg) fv)]
= match sg with
| [] -> ()
| hd::tl -> extend_env_l_lookup_fvar g tl fv
let rec elab_ty_soundness (g:RT.fstar_top_env)
(sg:stlc_env)
(t:stlc_ty)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_ty t) (RT.tm_type RT.u_zero))
(decreases t)
= match t with
| TUnit ->
RT.T_FVar _ RT.unit_fv
| TArrow t1 t2 ->
let t1_ok = elab_ty_soundness g sg t1 in
let x = fresh sg in
fresh_is_fresh sg;
elab_ty_freevars t2;
let t2_ok = elab_ty_soundness g ((x, t1)::sg) t2 in
let arr_max
: RT.tot_typing
(extend_env_l g sg)
(elab_ty t)
(RT.tm_type RT.(u_max u_zero u_zero))
= RT.T_Arrow _ x (elab_ty t1) (elab_ty t2)
_ _ RT.pp_name_default R.Q_Explicit T.E_Total _ _ t1_ok t2_ok
in
RT.simplify_umax arr_max
let rec elab_exp_freevars (e:stlc_exp)
: Lemma (freevars e `Set.equal` RT.freevars (elab_exp e))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam t e ->
elab_ty_freevars t;
elab_exp_freevars e
| EApp e1 e2 ->
elab_exp_freevars e1;
elab_exp_freevars e2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | dd: STLC.Core.stlc_typing sg se st -> g: FStar.Reflection.Typing.fstar_top_env
-> Prims.GTot
(FStar.Reflection.Typing.tot_typing (STLC.Core.extend_env_l g sg)
(STLC.Core.elab_exp se)
(STLC.Core.elab_ty st)) | Prims.GTot | [
"sometrivial",
""
] | [] | [
"STLC.Core.stlc_env",
"STLC.Core.stlc_exp",
"STLC.Core.stlc_ty",
"STLC.Core.stlc_typing",
"FStar.Reflection.Typing.fstar_top_env",
"FStar.Reflection.Typing.T_Const",
"STLC.Core.extend_env_l",
"FStar.Stubs.Reflection.V2.Data.C_Unit",
"FStar.Reflection.Typing.unit_ty",
"FStar.Reflection.Typing.CT_Unit",
"STLC.Core.var",
"Prims.b2t",
"FStar.Pervasives.Native.uu___is_Some",
"STLC.Core.lookup",
"FStar.Reflection.Typing.T_Var",
"FStar.Stubs.Reflection.V2.Builtins.pack_namedv",
"FStar.Reflection.Typing.make_namedv",
"Prims.l_and",
"FStar.Pervasives.Native.uu___is_None",
"Prims.l_not",
"FStar.Set.mem",
"STLC.Core.freevars",
"Prims.Cons",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.Mktuple2",
"STLC.Core.open_exp",
"FStar.Reflection.Typing.typing",
"FStar.Stubs.Reflection.V2.Builtins.pack_ln",
"FStar.Stubs.Reflection.V2.Data.Tv_Abs",
"FStar.Reflection.Typing.mk_binder",
"FStar.Reflection.Typing.pp_name_default",
"STLC.Core.elab_ty",
"FStar.Stubs.Reflection.V2.Data.Q_Explicit",
"STLC.Core.elab_exp",
"FStar.Stubs.TypeChecker.Core.tot_or_ghost",
"FStar.Stubs.Reflection.Types.typ",
"FStar.Stubs.TypeChecker.Core.E_Total",
"FStar.Stubs.Reflection.V2.Data.Tv_Arrow",
"FStar.Reflection.Typing.mk_comp",
"FStar.Reflection.Typing.close_comp_typ",
"FStar.Reflection.Typing.T_Abs",
"FStar.Reflection.Typing.u_zero",
"STLC.Core.elab_ty_soundness",
"Prims.unit",
"STLC.Core.elab_exp_freevars",
"STLC.Core.fresh_is_fresh",
"FStar.Reflection.Typing.tot_typing",
"FStar.Reflection.Typing.extend_env",
"STLC.Core.soundness",
"STLC.Core.TArrow",
"STLC.Core.EApp",
"FStar.Reflection.Typing.open_with",
"FStar.Reflection.Typing.T_App",
"FStar.Reflection.Typing.mk_simple_binder"
] | [
"recursion"
] | false | false | false | false | false | let rec soundness
(#sg: stlc_env)
(#se: stlc_exp)
(#st: stlc_ty)
(dd: stlc_typing sg se st)
(g: RT.fstar_top_env)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_exp se) (elab_ty st)) (decreases dd) =
| match dd with
| T_Unit _ -> RT.T_Const _ _ _ RT.CT_Unit
| T_Var _ x -> RT.T_Var _ (R.pack_namedv (RT.make_namedv x))
| T_Lam _ t e t' x de ->
let de:RT.tot_typing (extend_env_l g ((x, t) :: sg)) (elab_exp (open_exp e x)) (elab_ty t') =
soundness de g
in
let de:RT.tot_typing (RT.extend_env (extend_env_l g sg) x (elab_ty t))
(elab_exp (open_exp e x))
(elab_ty t') =
de
in
fresh_is_fresh sg;
elab_exp_freevars e;
let dd =
RT.T_Abs (extend_env_l g sg)
x
(elab_ty t)
(elab_exp e)
(T.E_Total, elab_ty t')
_
RT.pp_name_default
R.Q_Explicit
_
(elab_ty_soundness g sg t)
de
in
dd
| T_App _ e1 e2 t t' d1 d2 ->
let dt1:RT.tot_typing (extend_env_l g sg) (elab_exp e1) (elab_ty (TArrow t t')) =
soundness d1 g
in
let dt2:RT.tot_typing (extend_env_l g sg) (elab_exp e2) (elab_ty t) = soundness d2 g in
let dt:RT.tot_typing (extend_env_l g sg)
(elab_exp (EApp e1 e2))
(RT.open_with (elab_ty t') (elab_exp e2)) =
RT.T_App _ _ _ _ _ _ dt1 dt2
in
dt | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.parse_bcvli | val parse_bcvli : parser parse_bcvli_kind U32.t | val parse_bcvli : parser parse_bcvli_kind U32.t | let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 59,
"end_line": 34,
"start_col": 0,
"start_line": 33
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | LowParse.Spec.Base.parser LowParse.Spec.BCVLI.parse_bcvli_kind FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Combinators.and_then",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.BoundedInt.parse_bounded_integer_le",
"LowParse.Spec.BCVLI.parse_bcvli_payload_kind",
"FStar.UInt32.t",
"LowParse.Spec.BCVLI.parse_bcvli_payload",
"LowParse.Spec.Base.parser",
"LowParse.Spec.BCVLI.parse_bcvli_kind"
] | [] | false | false | false | true | false | let parse_bcvli:parser parse_bcvli_kind U32.t =
| (parse_bounded_integer_le 1) `and_then` parse_bcvli_payload | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.parse_bounded_bcvli' | val parse_bounded_bcvli' (min: nat) (max: nat{min <= max})
: Tot (parser (parse_filter_kind parse_bcvli_kind) (bounded_int32 min max)) | val parse_bounded_bcvli' (min: nat) (max: nat{min <= max})
: Tot (parser (parse_filter_kind parse_bcvli_kind) (bounded_int32 min max)) | let parse_bounded_bcvli'
(min: nat)
(max: nat { min <= max })
: Tot (parser (parse_filter_kind parse_bcvli_kind) (bounded_int32 min max))
= parse_filter parse_bcvli (in_bounds min max) | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 46,
"end_line": 106,
"start_col": 0,
"start_line": 102
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options
let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload
let parse_bcvli_eq
b
= and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b'
let bare_serialize_bcvli (x: U32.t) : GTot bytes =
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
Seq.append
(serialize (serialize_bounded_integer_le 1) c1)
(
if U32.v c1 <= 252 then Seq.empty else
if U32.v c1 = 253 then serialize (serialize_bounded_integer_le 2) x else
serialize (serialize_bounded_integer_le 4) x
)
#push-options "--z3rlimit 32"
let bare_serialize_bcvli_correct : squash
(serializer_correct parse_bcvli bare_serialize_bcvli)
= let prf (x: U32.t) : Lemma
(let y = bare_serialize_bcvli x in
parse parse_bcvli y == Some (x, Seq.length y))
= let y = bare_serialize_bcvli x in
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
let sc1 = serialize (serialize_bounded_integer_le 1) c1 in
parse_strong_prefix (parse_bounded_integer_le 1) sc1 y;
let y' = Seq.slice y (Seq.length sc1) (Seq.length y) in
parse_bcvli_eq y;
if U32.v c1 <= 252 then begin
assert (y `Seq.equal` sc1)
end else if U32.v c1 = 253 then begin
assert (U32.v x <= 65535);
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 2) x)
end else begin
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 4) x)
end
in
Classical.forall_intro (Classical.move_requires prf)
#pop-options
let serialize_bcvli : serializer parse_bcvli = bare_serialize_bcvli
let serialize_bcvli_eq x = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | min: Prims.nat -> max: Prims.nat{min <= max}
-> LowParse.Spec.Base.parser (LowParse.Spec.Combinators.parse_filter_kind LowParse.Spec.BCVLI.parse_bcvli_kind
)
(LowParse.Spec.BoundedInt.bounded_int32 min max) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.Spec.Combinators.parse_filter",
"LowParse.Spec.BCVLI.parse_bcvli_kind",
"FStar.UInt32.t",
"LowParse.Spec.BCVLI.parse_bcvli",
"LowParse.Spec.BoundedInt.in_bounds",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Combinators.parse_filter_kind",
"LowParse.Spec.BoundedInt.bounded_int32"
] | [] | false | false | false | false | false | let parse_bounded_bcvli' (min: nat) (max: nat{min <= max})
: Tot (parser (parse_filter_kind parse_bcvli_kind) (bounded_int32 min max)) =
| parse_filter parse_bcvli (in_bounds min max) | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.parse_bcvli_payload | val parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) | val parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) | let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 44,
"end_line": 19,
"start_col": 0,
"start_line": 15
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
} | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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: LowParse.Spec.BoundedInt.bounded_integer 1
-> LowParse.Spec.Base.parser LowParse.Spec.BCVLI.parse_bcvli_payload_kind FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.BoundedInt.bounded_integer",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"LowParse.Spec.Base.weaken",
"LowParse.Spec.BCVLI.parse_bcvli_payload_kind",
"LowParse.Spec.Combinators.parse_ret_kind",
"FStar.UInt32.t",
"LowParse.Spec.Combinators.parse_ret",
"Prims.bool",
"Prims.op_Equality",
"Prims.int",
"LowParse.Spec.Combinators.parse_filter_kind",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.Combinators.parse_synth",
"LowParse.Spec.Combinators.parse_filter_refine",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.Combinators.parse_filter",
"LowParse.Spec.BoundedInt.parse_bounded_integer_le",
"LowParse.Spec.Combinators.fail_parser",
"LowParse.Spec.Base.parser"
] | [] | false | false | false | false | false | let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
| if U32.v x <= 252
then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t))
else
if U32.v x = 253
then
weaken parse_bcvli_payload_kind
(((parse_bounded_integer_le 2) `parse_filter` (fun x -> U32.v x >= 253))
`parse_synth`
(fun x -> (x <: U32.t)))
else
if U32.v x = 254
then
weaken parse_bcvli_payload_kind
(((parse_bounded_integer_le 4) `parse_filter` (fun x -> U32.v x >= 65536))
`parse_synth`
(fun x -> (x <: U32.t)))
else fail_parser parse_bcvli_payload_kind U32.t | false |
STLC.Core.fst | STLC.Core.elab_open_commute' | val elab_open_commute' (e: stlc_exp) (x: var) (n: nat)
: Lemma
(ensures RT.subst_term (elab_exp e) (RT.open_with_var x n) == elab_exp (open_exp' e x n))
(decreases e) | val elab_open_commute' (e: stlc_exp) (x: var) (n: nat)
: Lemma
(ensures RT.subst_term (elab_exp e) (RT.open_with_var x n) == elab_exp (open_exp' e x n))
(decreases e) | let rec elab_open_commute' (e:stlc_exp) (x:var) (n:nat)
: Lemma (ensures
RT.subst_term (elab_exp e) (RT.open_with_var x n) ==
elab_exp (open_exp' e x n))
(decreases e)
= match e with
| EUnit -> ()
| EBVar _ -> ()
| EVar _ -> ()
| EApp e1 e2 ->
elab_open_commute' e1 x n;
elab_open_commute' e2 x n
| ELam t e ->
calc (==) {
elab_exp (open_exp' (ELam t e) x n);
(==) {}
elab_exp (ELam t (open_exp' e x (n + 1)));
(==) { }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp (open_exp' e x (n + 1)))));
(==) { elab_open_commute' e x (n + 1) }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(RT.subst_term (elab_exp e) RT.(open_with_var x (n + 1)))));
(==) { stlc_types_are_closed_core t (RT.open_with_var x n) }
RT.subst_term
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp e)))
RT.(open_with_var x n);
} | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 7,
"end_line": 405,
"start_col": 0,
"start_line": 379
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v
let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x
let stlc_types_are_closed3 (ty:stlc_ty) (x:R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.open_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.DT 0 (RT.var_as_term x) ];
RT.open_term_spec (elab_ty ty) x
let rec elab_ty_freevars (ty:stlc_ty)
: Lemma (RT.freevars (elab_ty ty) `Set.equal` Set.empty)
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
elab_ty_freevars t1;
elab_ty_freevars t2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: STLC.Core.stlc_exp -> x: STLC.Core.var -> n: Prims.nat
-> FStar.Pervasives.Lemma
(ensures
FStar.Reflection.Typing.subst_term (STLC.Core.elab_exp e)
(FStar.Reflection.Typing.open_with_var x n) ==
STLC.Core.elab_exp (STLC.Core.open_exp' e x n)) (decreases e) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"STLC.Core.stlc_exp",
"STLC.Core.var",
"Prims.nat",
"STLC.Core.index",
"STLC.Core.elab_open_commute'",
"Prims.unit",
"STLC.Core.stlc_ty",
"FStar.Calc.calc_finish",
"FStar.Stubs.Reflection.Types.term",
"Prims.eq2",
"STLC.Core.elab_exp",
"STLC.Core.open_exp'",
"STLC.Core.ELam",
"FStar.Reflection.Typing.subst_term",
"FStar.Stubs.Reflection.V2.Builtins.pack_ln",
"FStar.Stubs.Reflection.V2.Data.Tv_Abs",
"FStar.Reflection.Typing.mk_simple_binder",
"FStar.Reflection.Typing.pp_name_default",
"STLC.Core.elab_ty",
"FStar.Reflection.Typing.open_with_var",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"FStar.Calc.calc_step",
"Prims.op_Addition",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"STLC.Core.stlc_types_are_closed_core",
"Prims.l_True",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec elab_open_commute' (e: stlc_exp) (x: var) (n: nat)
: Lemma
(ensures RT.subst_term (elab_exp e) (RT.open_with_var x n) == elab_exp (open_exp' e x n))
(decreases e) =
| match e with
| EUnit -> ()
| EBVar _ -> ()
| EVar _ -> ()
| EApp e1 e2 ->
elab_open_commute' e1 x n;
elab_open_commute' e2 x n
| ELam t e ->
calc ( == ) {
elab_exp (open_exp' (ELam t e) x n);
( == ) { () }
elab_exp (ELam t (open_exp' e x (n + 1)));
( == ) { () }
let open R in
pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(elab_exp (open_exp' e x (n + 1))));
( == ) { elab_open_commute' e x (n + 1) }
let open R in
pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(RT.subst_term (elab_exp e) RT.(open_with_var x (n + 1))));
( == ) { stlc_types_are_closed_core t (RT.open_with_var x n) }
RT.subst_term R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(elab_exp e)))
RT.(open_with_var x n);
} | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.serialize_bounded_bcvli | val serialize_bounded_bcvli
(min: nat)
(max: nat { min <= max })
: Tot (serializer (parse_bounded_bcvli min max)) | val serialize_bounded_bcvli
(min: nat)
(max: nat { min <= max })
: Tot (serializer (parse_bounded_bcvli min max)) | let serialize_bounded_bcvli
min max
= assert_norm (parse_filter_refine (in_bounds min max) == bounded_int32 min max);
serialize_ext
_
(serialize_filter serialize_bcvli (in_bounds min max))
(parse_bounded_bcvli min max) | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 33,
"end_line": 145,
"start_col": 0,
"start_line": 139
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options
let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload
let parse_bcvli_eq
b
= and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b'
let bare_serialize_bcvli (x: U32.t) : GTot bytes =
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
Seq.append
(serialize (serialize_bounded_integer_le 1) c1)
(
if U32.v c1 <= 252 then Seq.empty else
if U32.v c1 = 253 then serialize (serialize_bounded_integer_le 2) x else
serialize (serialize_bounded_integer_le 4) x
)
#push-options "--z3rlimit 32"
let bare_serialize_bcvli_correct : squash
(serializer_correct parse_bcvli bare_serialize_bcvli)
= let prf (x: U32.t) : Lemma
(let y = bare_serialize_bcvli x in
parse parse_bcvli y == Some (x, Seq.length y))
= let y = bare_serialize_bcvli x in
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
let sc1 = serialize (serialize_bounded_integer_le 1) c1 in
parse_strong_prefix (parse_bounded_integer_le 1) sc1 y;
let y' = Seq.slice y (Seq.length sc1) (Seq.length y) in
parse_bcvli_eq y;
if U32.v c1 <= 252 then begin
assert (y `Seq.equal` sc1)
end else if U32.v c1 = 253 then begin
assert (U32.v x <= 65535);
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 2) x)
end else begin
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 4) x)
end
in
Classical.forall_intro (Classical.move_requires prf)
#pop-options
let serialize_bcvli : serializer parse_bcvli = bare_serialize_bcvli
let serialize_bcvli_eq x = ()
let parse_bounded_bcvli'
(min: nat)
(max: nat { min <= max })
: Tot (parser (parse_filter_kind parse_bcvli_kind) (bounded_int32 min max))
= parse_filter parse_bcvli (in_bounds min max)
let parse_bounded_bcvli_size_correct
(min: nat)
(max: nat { min <= max })
: Lemma
(parse_bounded_bcvli_size min `parses_at_least` parse_bounded_bcvli' min max /\
parse_bounded_bcvli_size max `parses_at_most` parse_bounded_bcvli' min max)
= Classical.forall_intro (parse_filter_eq parse_bcvli (in_bounds min max));
Classical.forall_intro parse_bcvli_eq;
parser_kind_prop_equiv (parse_bounded_integer_kind 1) (parse_bounded_integer_le 1);
parser_kind_prop_equiv (parse_bounded_integer_kind 2) (parse_bounded_integer_le 2);
parser_kind_prop_equiv (parse_bounded_integer_kind 4) (parse_bounded_integer_le 4)
let parse_bounded_bcvli_kind_correct
(min: nat)
(max: nat { min <= max })
: Lemma
(parser_kind_prop (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max))
= parse_bounded_bcvli_size_correct min max;
parser_kind_prop_equiv (parse_filter_kind parse_bcvli_kind) (parse_bounded_bcvli' min max);
parser_kind_prop_equiv (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max)
let parse_bounded_bcvli
min max
= parse_bounded_bcvli_kind_correct min max;
strengthen (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max)
let parse_bounded_bcvli_eq
min max input
= parse_filter_eq parse_bcvli (in_bounds min max) input;
parse_bcvli_eq input | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | min: Prims.nat -> max: Prims.nat{min <= max}
-> LowParse.Spec.Base.serializer (LowParse.Spec.BCVLI.parse_bounded_bcvli min max) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.Spec.Base.serialize_ext",
"LowParse.Spec.Combinators.parse_filter_kind",
"LowParse.Spec.BCVLI.parse_bcvli_kind",
"LowParse.Spec.Combinators.parse_filter_refine",
"FStar.UInt32.t",
"LowParse.Spec.BoundedInt.in_bounds",
"LowParse.Spec.Combinators.parse_filter",
"LowParse.Spec.BCVLI.parse_bcvli",
"LowParse.Spec.Combinators.serialize_filter",
"LowParse.Spec.BCVLI.serialize_bcvli",
"LowParse.Spec.BCVLI.parse_bounded_bcvli_kind",
"LowParse.Spec.BoundedInt.bounded_int32",
"LowParse.Spec.BCVLI.parse_bounded_bcvli",
"Prims.unit",
"FStar.Pervasives.assert_norm",
"Prims.eq2",
"LowParse.Spec.Base.serializer"
] | [] | false | false | false | false | false | let serialize_bounded_bcvli min max =
| assert_norm (parse_filter_refine (in_bounds min max) == bounded_int32 min max);
serialize_ext _ (serialize_filter serialize_bcvli (in_bounds min max)) (parse_bounded_bcvli min max) | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.serialize_bcvli | val serialize_bcvli : serializer parse_bcvli | val serialize_bcvli : serializer parse_bcvli | let serialize_bcvli : serializer parse_bcvli = bare_serialize_bcvli | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 67,
"end_line": 98,
"start_col": 0,
"start_line": 98
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options
let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload
let parse_bcvli_eq
b
= and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b'
let bare_serialize_bcvli (x: U32.t) : GTot bytes =
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
Seq.append
(serialize (serialize_bounded_integer_le 1) c1)
(
if U32.v c1 <= 252 then Seq.empty else
if U32.v c1 = 253 then serialize (serialize_bounded_integer_le 2) x else
serialize (serialize_bounded_integer_le 4) x
)
#push-options "--z3rlimit 32"
let bare_serialize_bcvli_correct : squash
(serializer_correct parse_bcvli bare_serialize_bcvli)
= let prf (x: U32.t) : Lemma
(let y = bare_serialize_bcvli x in
parse parse_bcvli y == Some (x, Seq.length y))
= let y = bare_serialize_bcvli x in
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
let sc1 = serialize (serialize_bounded_integer_le 1) c1 in
parse_strong_prefix (parse_bounded_integer_le 1) sc1 y;
let y' = Seq.slice y (Seq.length sc1) (Seq.length y) in
parse_bcvli_eq y;
if U32.v c1 <= 252 then begin
assert (y `Seq.equal` sc1)
end else if U32.v c1 = 253 then begin
assert (U32.v x <= 65535);
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 2) x)
end else begin
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 4) x)
end
in
Classical.forall_intro (Classical.move_requires prf)
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | LowParse.Spec.Base.serializer LowParse.Spec.BCVLI.parse_bcvli | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.BCVLI.bare_serialize_bcvli",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.BCVLI.parse_bcvli_kind",
"FStar.UInt32.t",
"LowParse.Spec.BCVLI.parse_bcvli"
] | [] | false | false | false | true | false | let serialize_bcvli:serializer parse_bcvli =
| bare_serialize_bcvli | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.bare_serialize_bcvli | val bare_serialize_bcvli (x: U32.t) : GTot bytes | val bare_serialize_bcvli (x: U32.t) : GTot bytes | let bare_serialize_bcvli (x: U32.t) : GTot bytes =
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
Seq.append
(serialize (serialize_bounded_integer_le 1) c1)
(
if U32.v c1 <= 252 then Seq.empty else
if U32.v c1 = 253 then serialize (serialize_bounded_integer_le 2) x else
serialize (serialize_bounded_integer_le 4) x
) | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 63,
"start_col": 0,
"start_line": 48
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options
let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload
let parse_bcvli_eq
b
= and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b' | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt32.t -> Prims.GTot LowParse.Bytes.bytes | Prims.GTot | [
"sometrivial"
] | [] | [
"FStar.UInt32.t",
"FStar.Seq.Base.append",
"LowParse.Bytes.byte",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.BoundedInt.parse_bounded_integer_le",
"LowParse.Spec.BoundedInt.serialize_bounded_integer_le",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"FStar.Seq.Base.empty",
"Prims.bool",
"Prims.op_Equality",
"Prims.int",
"FStar.Seq.Base.seq",
"FStar.UInt32.__uint_to_t",
"LowParse.Bytes.bytes"
] | [] | false | false | false | false | false | let bare_serialize_bcvli (x: U32.t) : GTot bytes =
| let c1:bounded_integer 1 =
if U32.v x <= 252 then x else if U32.v x <= 65535 then 253ul else 254ul
in
Seq.append (serialize (serialize_bounded_integer_le 1) c1)
(if U32.v c1 <= 252
then Seq.empty
else
if U32.v c1 = 253
then serialize (serialize_bounded_integer_le 2) x
else serialize (serialize_bounded_integer_le 4) x) | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.parse_bounded_bcvli_eq | val parse_bounded_bcvli_eq
(min: nat)
(max: nat { min <= max })
(input: bytes)
: Lemma
(parse (parse_bounded_bcvli min max) input == (
match parse parse_bcvli input with
| Some (n, consumed) ->
if in_bounds min max n
then Some (n, consumed)
else None
| _ -> None
)) | val parse_bounded_bcvli_eq
(min: nat)
(max: nat { min <= max })
(input: bytes)
: Lemma
(parse (parse_bounded_bcvli min max) input == (
match parse parse_bcvli input with
| Some (n, consumed) ->
if in_bounds min max n
then Some (n, consumed)
else None
| _ -> None
)) | let parse_bounded_bcvli_eq
min max input
= parse_filter_eq parse_bcvli (in_bounds min max) input;
parse_bcvli_eq input | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 22,
"end_line": 137,
"start_col": 0,
"start_line": 134
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options
let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload
let parse_bcvli_eq
b
= and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b'
let bare_serialize_bcvli (x: U32.t) : GTot bytes =
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
Seq.append
(serialize (serialize_bounded_integer_le 1) c1)
(
if U32.v c1 <= 252 then Seq.empty else
if U32.v c1 = 253 then serialize (serialize_bounded_integer_le 2) x else
serialize (serialize_bounded_integer_le 4) x
)
#push-options "--z3rlimit 32"
let bare_serialize_bcvli_correct : squash
(serializer_correct parse_bcvli bare_serialize_bcvli)
= let prf (x: U32.t) : Lemma
(let y = bare_serialize_bcvli x in
parse parse_bcvli y == Some (x, Seq.length y))
= let y = bare_serialize_bcvli x in
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
let sc1 = serialize (serialize_bounded_integer_le 1) c1 in
parse_strong_prefix (parse_bounded_integer_le 1) sc1 y;
let y' = Seq.slice y (Seq.length sc1) (Seq.length y) in
parse_bcvli_eq y;
if U32.v c1 <= 252 then begin
assert (y `Seq.equal` sc1)
end else if U32.v c1 = 253 then begin
assert (U32.v x <= 65535);
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 2) x)
end else begin
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 4) x)
end
in
Classical.forall_intro (Classical.move_requires prf)
#pop-options
let serialize_bcvli : serializer parse_bcvli = bare_serialize_bcvli
let serialize_bcvli_eq x = ()
let parse_bounded_bcvli'
(min: nat)
(max: nat { min <= max })
: Tot (parser (parse_filter_kind parse_bcvli_kind) (bounded_int32 min max))
= parse_filter parse_bcvli (in_bounds min max)
let parse_bounded_bcvli_size_correct
(min: nat)
(max: nat { min <= max })
: Lemma
(parse_bounded_bcvli_size min `parses_at_least` parse_bounded_bcvli' min max /\
parse_bounded_bcvli_size max `parses_at_most` parse_bounded_bcvli' min max)
= Classical.forall_intro (parse_filter_eq parse_bcvli (in_bounds min max));
Classical.forall_intro parse_bcvli_eq;
parser_kind_prop_equiv (parse_bounded_integer_kind 1) (parse_bounded_integer_le 1);
parser_kind_prop_equiv (parse_bounded_integer_kind 2) (parse_bounded_integer_le 2);
parser_kind_prop_equiv (parse_bounded_integer_kind 4) (parse_bounded_integer_le 4)
let parse_bounded_bcvli_kind_correct
(min: nat)
(max: nat { min <= max })
: Lemma
(parser_kind_prop (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max))
= parse_bounded_bcvli_size_correct min max;
parser_kind_prop_equiv (parse_filter_kind parse_bcvli_kind) (parse_bounded_bcvli' min max);
parser_kind_prop_equiv (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max)
let parse_bounded_bcvli
min max
= parse_bounded_bcvli_kind_correct min max;
strengthen (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | min: Prims.nat -> max: Prims.nat{min <= max} -> input: LowParse.Bytes.bytes
-> FStar.Pervasives.Lemma
(ensures
LowParse.Spec.Base.parse (LowParse.Spec.BCVLI.parse_bounded_bcvli min max) input ==
(match LowParse.Spec.Base.parse LowParse.Spec.BCVLI.parse_bcvli input with
| FStar.Pervasives.Native.Some #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ n consumed) ->
(match LowParse.Spec.BoundedInt.in_bounds min max n with
| true -> FStar.Pervasives.Native.Some (n, consumed)
| _ -> FStar.Pervasives.Native.None)
<:
FStar.Pervasives.Native.option (LowParse.Spec.BoundedInt.bounded_int32 min max *
LowParse.Spec.Base.consumed_length input)
| _ -> FStar.Pervasives.Native.None)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.Bytes.bytes",
"LowParse.Spec.BCVLI.parse_bcvli_eq",
"Prims.unit",
"LowParse.Spec.Combinators.parse_filter_eq",
"LowParse.Spec.BCVLI.parse_bcvli_kind",
"FStar.UInt32.t",
"LowParse.Spec.BCVLI.parse_bcvli",
"LowParse.Spec.BoundedInt.in_bounds"
] | [] | true | false | true | false | false | let parse_bounded_bcvli_eq min max input =
| parse_filter_eq parse_bcvli (in_bounds min max) input;
parse_bcvli_eq input | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.parse_bounded_bcvli_kind_correct | val parse_bounded_bcvli_kind_correct (min: nat) (max: nat{min <= max})
: Lemma (parser_kind_prop (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max)) | val parse_bounded_bcvli_kind_correct (min: nat) (max: nat{min <= max})
: Lemma (parser_kind_prop (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max)) | let parse_bounded_bcvli_kind_correct
(min: nat)
(max: nat { min <= max })
: Lemma
(parser_kind_prop (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max))
= parse_bounded_bcvli_size_correct min max;
parser_kind_prop_equiv (parse_filter_kind parse_bcvli_kind) (parse_bounded_bcvli' min max);
parser_kind_prop_equiv (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max) | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 90,
"end_line": 127,
"start_col": 0,
"start_line": 120
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options
let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload
let parse_bcvli_eq
b
= and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b'
let bare_serialize_bcvli (x: U32.t) : GTot bytes =
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
Seq.append
(serialize (serialize_bounded_integer_le 1) c1)
(
if U32.v c1 <= 252 then Seq.empty else
if U32.v c1 = 253 then serialize (serialize_bounded_integer_le 2) x else
serialize (serialize_bounded_integer_le 4) x
)
#push-options "--z3rlimit 32"
let bare_serialize_bcvli_correct : squash
(serializer_correct parse_bcvli bare_serialize_bcvli)
= let prf (x: U32.t) : Lemma
(let y = bare_serialize_bcvli x in
parse parse_bcvli y == Some (x, Seq.length y))
= let y = bare_serialize_bcvli x in
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
let sc1 = serialize (serialize_bounded_integer_le 1) c1 in
parse_strong_prefix (parse_bounded_integer_le 1) sc1 y;
let y' = Seq.slice y (Seq.length sc1) (Seq.length y) in
parse_bcvli_eq y;
if U32.v c1 <= 252 then begin
assert (y `Seq.equal` sc1)
end else if U32.v c1 = 253 then begin
assert (U32.v x <= 65535);
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 2) x)
end else begin
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 4) x)
end
in
Classical.forall_intro (Classical.move_requires prf)
#pop-options
let serialize_bcvli : serializer parse_bcvli = bare_serialize_bcvli
let serialize_bcvli_eq x = ()
let parse_bounded_bcvli'
(min: nat)
(max: nat { min <= max })
: Tot (parser (parse_filter_kind parse_bcvli_kind) (bounded_int32 min max))
= parse_filter parse_bcvli (in_bounds min max)
let parse_bounded_bcvli_size_correct
(min: nat)
(max: nat { min <= max })
: Lemma
(parse_bounded_bcvli_size min `parses_at_least` parse_bounded_bcvli' min max /\
parse_bounded_bcvli_size max `parses_at_most` parse_bounded_bcvli' min max)
= Classical.forall_intro (parse_filter_eq parse_bcvli (in_bounds min max));
Classical.forall_intro parse_bcvli_eq;
parser_kind_prop_equiv (parse_bounded_integer_kind 1) (parse_bounded_integer_le 1);
parser_kind_prop_equiv (parse_bounded_integer_kind 2) (parse_bounded_integer_le 2);
parser_kind_prop_equiv (parse_bounded_integer_kind 4) (parse_bounded_integer_le 4) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | min: Prims.nat -> max: Prims.nat{min <= max}
-> FStar.Pervasives.Lemma
(ensures
LowParse.Spec.Base.parser_kind_prop (LowParse.Spec.BCVLI.parse_bounded_bcvli_kind min max)
(LowParse.Spec.BCVLI.parse_bounded_bcvli' min max)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"LowParse.Spec.BoundedInt.bounded_int32",
"LowParse.Spec.BCVLI.parse_bounded_bcvli_kind",
"LowParse.Spec.BCVLI.parse_bounded_bcvli'",
"Prims.unit",
"LowParse.Spec.Combinators.parse_filter_kind",
"LowParse.Spec.BCVLI.parse_bcvli_kind",
"LowParse.Spec.BCVLI.parse_bounded_bcvli_size_correct",
"Prims.l_True",
"Prims.squash",
"LowParse.Spec.Base.parser_kind_prop",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let parse_bounded_bcvli_kind_correct (min: nat) (max: nat{min <= max})
: Lemma (parser_kind_prop (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max)) =
| parse_bounded_bcvli_size_correct min max;
parser_kind_prop_equiv (parse_filter_kind parse_bcvli_kind) (parse_bounded_bcvli' min max);
parser_kind_prop_equiv (parse_bounded_bcvli_kind min max) (parse_bounded_bcvli' min max) | false |
Steel.Utils.fst | Steel.Utils.emp_unit | val emp_unit (p: vprop)
: Lemma (((p `star` emp) `equiv` p) /\ ((emp `star` p) `equiv` p))
[SMTPatOr [[SMTPat (p `star` emp)]; [SMTPat (emp `star` p)]]] | val emp_unit (p: vprop)
: Lemma (((p `star` emp) `equiv` p) /\ ((emp `star` p) `equiv` p))
[SMTPatOr [[SMTPat (p `star` emp)]; [SMTPat (emp `star` p)]]] | let emp_unit (p:vprop)
: Lemma (((p `star` emp) `equiv` p) /\
((emp `star` p) `equiv` p))
[SMTPatOr [[SMTPat (p `star` emp)];
[SMTPat (emp `star` p)]]]
= reveal_equiv (p `star` emp) p;
reveal_equiv (emp `star` p) p;
reveal_emp ();
Steel.Memory.emp_unit (hp_of p);
Steel.Memory.star_commutative Steel.Memory.emp (hp_of p) | {
"file_name": "lib/steel/Steel.Utils.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 60,
"end_line": 75,
"start_col": 0,
"start_line": 66
} | (*
Copyright 2020 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 Steel.Utils
open Steel.Memory
open Steel.Effect.Atomic
open Steel.Effect
open Steel.FractionalPermission
open Steel.Reference
let pts_to_not_null (#a:Type)
(#opened:inames)
(#p:perm)
(#v:FStar.Ghost.erased a)
(r:ref a)
: SteelGhost unit opened
(pts_to r p v)
(fun _ -> pts_to r p v)
(fun _ -> True)
(fun _ _ _ -> r =!= null)
= extract_info_raw (pts_to r p v) (r =!= null)
(fun m -> pts_to_not_null r p v m)
let change_slprop_ens (p:vprop) (q:vprop) (r:prop) (f:(m:mem -> Lemma (requires interp (hp_of p) m)
(ensures interp (hp_of q) m /\ r)))
: Steel unit p (fun _ -> q) (requires fun _ -> True) (ensures fun _ _ _ -> r)
= rewrite_slprop p (q `star` pure r)
(fun m -> f m;
Steel.Memory.emp_unit (hp_of q);
Steel.Memory.pure_star_interp (hp_of q) r m);
elim_pure r
let pure_as_ens (#p:prop) ()
: Steel unit (pure p) (fun _ -> pure p) (fun _ -> True) (fun _ _ _ -> p)
= change_slprop_ens (pure p) (pure p) p (Steel.Memory.pure_interp p)
let rewrite #a (#p:a -> vprop) (x y:a)
: Steel unit (p x) (fun _ -> p y)
(requires fun _ -> x == y)
(ensures fun _ _ _ -> True)
= rewrite_slprop (p x) (p y) (fun _ -> ())
let extract_pure (p:prop)
: Steel unit (pure p) (fun _ -> pure p) (fun _ -> True) (fun _ _ _ -> p)
= elim_pure p;
intro_pure p
let dup_pure (p:prop)
: SteelT unit (pure p) (fun _ -> pure p `star` pure p)
= extract_pure p;
intro_pure p | {
"checked_file": "/",
"dependencies": [
"Steel.Reference.fsti.checked",
"Steel.Memory.fsti.checked",
"Steel.FractionalPermission.fst.checked",
"Steel.Effect.Atomic.fsti.checked",
"Steel.Effect.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Steel.Utils.fst"
} | [
{
"abbrev": false,
"full_module": "Steel.Reference",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.FractionalPermission",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect.Atomic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel",
"short_module": null
},
{
"abbrev": 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: Steel.Effect.Common.vprop
-> FStar.Pervasives.Lemma
(ensures
Steel.Effect.Common.equiv (Steel.Effect.Common.star p Steel.Effect.Common.emp) p /\
Steel.Effect.Common.equiv (Steel.Effect.Common.star Steel.Effect.Common.emp p) p)
[
SMTPatOr [
[SMTPat (Steel.Effect.Common.star p Steel.Effect.Common.emp)];
[SMTPat (Steel.Effect.Common.star Steel.Effect.Common.emp p)]
]
] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Memory.star_commutative",
"Steel.Memory.emp",
"Steel.Effect.Common.hp_of",
"Prims.unit",
"Steel.Memory.emp_unit",
"Steel.Effect.Common.reveal_emp",
"Steel.Effect.Common.reveal_equiv",
"Steel.Effect.Common.star",
"Steel.Effect.Common.emp",
"Prims.l_True",
"Prims.squash",
"Prims.l_and",
"Steel.Effect.Common.equiv",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat_or",
"Prims.list",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [] | true | false | true | false | false | let emp_unit (p: vprop)
: Lemma (((p `star` emp) `equiv` p) /\ ((emp `star` p) `equiv` p))
[SMTPatOr [[SMTPat (p `star` emp)]; [SMTPat (emp `star` p)]]] =
| reveal_equiv (p `star` emp) p;
reveal_equiv (emp `star` p) p;
reveal_emp ();
Steel.Memory.emp_unit (hp_of p);
Steel.Memory.star_commutative Steel.Memory.emp (hp_of p) | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.bare_serialize_bcvli_correct | val bare_serialize_bcvli_correct:squash (serializer_correct parse_bcvli bare_serialize_bcvli) | val bare_serialize_bcvli_correct:squash (serializer_correct parse_bcvli bare_serialize_bcvli) | let bare_serialize_bcvli_correct : squash
(serializer_correct parse_bcvli bare_serialize_bcvli)
= let prf (x: U32.t) : Lemma
(let y = bare_serialize_bcvli x in
parse parse_bcvli y == Some (x, Seq.length y))
= let y = bare_serialize_bcvli x in
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
let sc1 = serialize (serialize_bounded_integer_le 1) c1 in
parse_strong_prefix (parse_bounded_integer_le 1) sc1 y;
let y' = Seq.slice y (Seq.length sc1) (Seq.length y) in
parse_bcvli_eq y;
if U32.v c1 <= 252 then begin
assert (y `Seq.equal` sc1)
end else if U32.v c1 = 253 then begin
assert (U32.v x <= 65535);
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 2) x)
end else begin
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 4) x)
end
in
Classical.forall_intro (Classical.move_requires prf) | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 54,
"end_line": 94,
"start_col": 0,
"start_line": 67
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options
let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload
let parse_bcvli_eq
b
= and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b'
let bare_serialize_bcvli (x: U32.t) : GTot bytes =
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
Seq.append
(serialize (serialize_bounded_integer_le 1) c1)
(
if U32.v c1 <= 252 then Seq.empty else
if U32.v c1 = 253 then serialize (serialize_bounded_integer_le 2) x else
serialize (serialize_bounded_integer_le 4) x
)
#push-options "--z3rlimit 32" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.squash (LowParse.Spec.Base.serializer_correct LowParse.Spec.BCVLI.parse_bcvli
LowParse.Spec.BCVLI.bare_serialize_bcvli) | Prims.Tot | [
"total"
] | [] | [
"FStar.Classical.forall_intro",
"FStar.UInt32.t",
"Prims.l_imp",
"Prims.l_True",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.BCVLI.bare_serialize_bcvli",
"LowParse.Spec.Base.parse",
"LowParse.Spec.BCVLI.parse_bcvli",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"FStar.Classical.move_requires",
"Prims.unit",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"Prims._assert",
"FStar.Seq.Base.equal",
"Prims.bool",
"Prims.op_Equality",
"Prims.int",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.BoundedInt.parse_bounded_integer_le",
"LowParse.Spec.BoundedInt.serialize_bounded_integer_le",
"Prims.b2t",
"LowParse.Spec.BCVLI.parse_bcvli_eq",
"FStar.Seq.Base.seq",
"FStar.Seq.Base.slice",
"LowParse.Spec.Base.parse_strong_prefix",
"LowParse.Bytes.bytes",
"FStar.UInt32.__uint_to_t"
] | [] | false | false | true | true | false | let bare_serialize_bcvli_correct:squash (serializer_correct parse_bcvli bare_serialize_bcvli) =
| let prf (x: U32.t)
: Lemma
(let y = bare_serialize_bcvli x in
parse parse_bcvli y == Some (x, Seq.length y)) =
let y = bare_serialize_bcvli x in
let c1:bounded_integer 1 =
if U32.v x <= 252 then x else if U32.v x <= 65535 then 253ul else 254ul
in
let sc1 = serialize (serialize_bounded_integer_le 1) c1 in
parse_strong_prefix (parse_bounded_integer_le 1) sc1 y;
let y' = Seq.slice y (Seq.length sc1) (Seq.length y) in
parse_bcvli_eq y;
if U32.v c1 <= 252
then assert (y `Seq.equal` sc1)
else
if U32.v c1 = 253
then
(assert (U32.v x <= 65535);
assert (y' `Seq.equal` (serialize (serialize_bounded_integer_le 2) x)))
else assert (y' `Seq.equal` (serialize (serialize_bounded_integer_le 4) x))
in
Classical.forall_intro (Classical.move_requires prf) | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.parse_bounded_bcvli_size_correct | val parse_bounded_bcvli_size_correct (min: nat) (max: nat{min <= max})
: Lemma
((parse_bounded_bcvli_size min) `parses_at_least` (parse_bounded_bcvli' min max) /\
(parse_bounded_bcvli_size max) `parses_at_most` (parse_bounded_bcvli' min max)) | val parse_bounded_bcvli_size_correct (min: nat) (max: nat{min <= max})
: Lemma
((parse_bounded_bcvli_size min) `parses_at_least` (parse_bounded_bcvli' min max) /\
(parse_bounded_bcvli_size max) `parses_at_most` (parse_bounded_bcvli' min max)) | let parse_bounded_bcvli_size_correct
(min: nat)
(max: nat { min <= max })
: Lemma
(parse_bounded_bcvli_size min `parses_at_least` parse_bounded_bcvli' min max /\
parse_bounded_bcvli_size max `parses_at_most` parse_bounded_bcvli' min max)
= Classical.forall_intro (parse_filter_eq parse_bcvli (in_bounds min max));
Classical.forall_intro parse_bcvli_eq;
parser_kind_prop_equiv (parse_bounded_integer_kind 1) (parse_bounded_integer_le 1);
parser_kind_prop_equiv (parse_bounded_integer_kind 2) (parse_bounded_integer_le 2);
parser_kind_prop_equiv (parse_bounded_integer_kind 4) (parse_bounded_integer_le 4) | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 84,
"end_line": 118,
"start_col": 0,
"start_line": 108
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options
let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload
let parse_bcvli_eq
b
= and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b'
let bare_serialize_bcvli (x: U32.t) : GTot bytes =
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
Seq.append
(serialize (serialize_bounded_integer_le 1) c1)
(
if U32.v c1 <= 252 then Seq.empty else
if U32.v c1 = 253 then serialize (serialize_bounded_integer_le 2) x else
serialize (serialize_bounded_integer_le 4) x
)
#push-options "--z3rlimit 32"
let bare_serialize_bcvli_correct : squash
(serializer_correct parse_bcvli bare_serialize_bcvli)
= let prf (x: U32.t) : Lemma
(let y = bare_serialize_bcvli x in
parse parse_bcvli y == Some (x, Seq.length y))
= let y = bare_serialize_bcvli x in
let c1 : bounded_integer 1 =
if U32.v x <= 252 then
x
else if U32.v x <= 65535 then
253ul
else
254ul
in
let sc1 = serialize (serialize_bounded_integer_le 1) c1 in
parse_strong_prefix (parse_bounded_integer_le 1) sc1 y;
let y' = Seq.slice y (Seq.length sc1) (Seq.length y) in
parse_bcvli_eq y;
if U32.v c1 <= 252 then begin
assert (y `Seq.equal` sc1)
end else if U32.v c1 = 253 then begin
assert (U32.v x <= 65535);
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 2) x)
end else begin
assert (y' `Seq.equal` serialize (serialize_bounded_integer_le 4) x)
end
in
Classical.forall_intro (Classical.move_requires prf)
#pop-options
let serialize_bcvli : serializer parse_bcvli = bare_serialize_bcvli
let serialize_bcvli_eq x = ()
let parse_bounded_bcvli'
(min: nat)
(max: nat { min <= max })
: Tot (parser (parse_filter_kind parse_bcvli_kind) (bounded_int32 min max))
= parse_filter parse_bcvli (in_bounds min max) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | min: Prims.nat -> max: Prims.nat{min <= max}
-> FStar.Pervasives.Lemma
(ensures
LowParse.Spec.Base.parses_at_least (LowParse.Spec.BCVLI.parse_bounded_bcvli_size min)
(LowParse.Spec.BCVLI.parse_bounded_bcvli' min max) /\
LowParse.Spec.Base.parses_at_most (LowParse.Spec.BCVLI.parse_bounded_bcvli_size max)
(LowParse.Spec.BCVLI.parse_bounded_bcvli' min max)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.BoundedInt.parse_bounded_integer_le",
"Prims.unit",
"FStar.Classical.forall_intro",
"LowParse.Bytes.bytes",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"FStar.UInt32.t",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"LowParse.Spec.BCVLI.parse_bcvli",
"FStar.Pervasives.Native.None",
"FStar.UInt32.v",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"Prims.bool",
"Prims.op_Equality",
"Prims.int",
"FStar.Seq.Base.slice",
"LowParse.Bytes.byte",
"FStar.Seq.Base.length",
"Prims.op_LessThan",
"Prims.op_Addition",
"LowParse.Spec.BCVLI.parse_bcvli_eq",
"LowParse.Spec.Combinators.parse_filter_refine",
"LowParse.Spec.BoundedInt.in_bounds",
"LowParse.Spec.Combinators.parse_filter",
"LowParse.Spec.BCVLI.parse_bcvli_kind",
"LowParse.Spec.Combinators.parse_filter_eq",
"Prims.l_True",
"Prims.squash",
"Prims.l_and",
"LowParse.Spec.Base.parses_at_least",
"LowParse.Spec.BCVLI.parse_bounded_bcvli_size",
"LowParse.Spec.BoundedInt.bounded_int32",
"LowParse.Spec.BCVLI.parse_bounded_bcvli'",
"LowParse.Spec.Base.parses_at_most",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let parse_bounded_bcvli_size_correct (min: nat) (max: nat{min <= max})
: Lemma
((parse_bounded_bcvli_size min) `parses_at_least` (parse_bounded_bcvli' min max) /\
(parse_bounded_bcvli_size max) `parses_at_most` (parse_bounded_bcvli' min max)) =
| Classical.forall_intro (parse_filter_eq parse_bcvli (in_bounds min max));
Classical.forall_intro parse_bcvli_eq;
parser_kind_prop_equiv (parse_bounded_integer_kind 1) (parse_bounded_integer_le 1);
parser_kind_prop_equiv (parse_bounded_integer_kind 2) (parse_bounded_integer_le 2);
parser_kind_prop_equiv (parse_bounded_integer_kind 4) (parse_bounded_integer_le 4) | false |
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.parse_bcvli_eq | val parse_bcvli_eq (b: bytes) : Lemma
(parse parse_bcvli b == (match parse (parse_bounded_integer_le 1) b with
| None -> None
| Some (x32, consumed_x) ->
let x = U32.v x32 in
if x <= 252 then Some (x32, consumed_x) else
let b' = Seq.slice b consumed_x (Seq.length b) in
if x = 253 then
match parse (parse_bounded_integer_le 2) b' with
| None -> None
| Some (y, consumed_y) ->
if U32.v y < 253 then None (* redundant representations not supported, non-malleability *) else Some (y, consumed_x + consumed_y)
else if x = 254 then
match parse (parse_bounded_integer_le 4) b' with
| None -> None
| Some (y, consumed_y) ->
if U32.v y < 65536 then None (* redundant representations not supported, non-malleability *) else Some (y, consumed_x + consumed_y)
else None (* 64-bit integers not supported *)
)) | val parse_bcvli_eq (b: bytes) : Lemma
(parse parse_bcvli b == (match parse (parse_bounded_integer_le 1) b with
| None -> None
| Some (x32, consumed_x) ->
let x = U32.v x32 in
if x <= 252 then Some (x32, consumed_x) else
let b' = Seq.slice b consumed_x (Seq.length b) in
if x = 253 then
match parse (parse_bounded_integer_le 2) b' with
| None -> None
| Some (y, consumed_y) ->
if U32.v y < 253 then None (* redundant representations not supported, non-malleability *) else Some (y, consumed_x + consumed_y)
else if x = 254 then
match parse (parse_bounded_integer_le 4) b' with
| None -> None
| Some (y, consumed_y) ->
if U32.v y < 65536 then None (* redundant representations not supported, non-malleability *) else Some (y, consumed_x + consumed_y)
else None (* 64-bit integers not supported *)
)) | let parse_bcvli_eq
b
= and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b' | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 79,
"end_line": 46,
"start_col": 0,
"start_line": 36
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32"
let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
)
#pop-options
let parse_bcvli : parser parse_bcvli_kind U32.t =
parse_bounded_integer_le 1 `and_then` parse_bcvli_payload | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | b: LowParse.Bytes.bytes
-> FStar.Pervasives.Lemma
(ensures
LowParse.Spec.Base.parse LowParse.Spec.BCVLI.parse_bcvli b ==
(match LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer_le 1) b with
| FStar.Pervasives.Native.None #_ -> FStar.Pervasives.Native.None
| FStar.Pervasives.Native.Some #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ x32 consumed_x) ->
let x = FStar.UInt32.v x32 in
(match x <= 252 with
| true -> FStar.Pervasives.Native.Some (x32, consumed_x)
| _ ->
let b' = FStar.Seq.Base.slice b consumed_x (FStar.Seq.Base.length b) in
(match x = 253 with
| true ->
(match
LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer_le 2)
b'
with
| FStar.Pervasives.Native.None #_ -> FStar.Pervasives.Native.None
| FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ y consumed_y) ->
(match FStar.UInt32.v y < 253 with
| true -> FStar.Pervasives.Native.None
| _ -> FStar.Pervasives.Native.Some (y, consumed_x + consumed_y))
<:
FStar.Pervasives.Native.option (FStar.UInt32.t *
LowParse.Spec.Base.consumed_length b))
<:
FStar.Pervasives.Native.option (FStar.UInt32.t *
LowParse.Spec.Base.consumed_length b)
| _ ->
(match x = 254 with
| true ->
(match
LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer_le
4)
b'
with
| FStar.Pervasives.Native.None #_ -> FStar.Pervasives.Native.None
| FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ y consumed_y) ->
(match FStar.UInt32.v y < 65536 with
| true -> FStar.Pervasives.Native.None
| _ -> FStar.Pervasives.Native.Some (y, consumed_x + consumed_y))
<:
FStar.Pervasives.Native.option (FStar.UInt32.t *
LowParse.Spec.Base.consumed_length b))
<:
FStar.Pervasives.Native.option (FStar.UInt32.t *
LowParse.Spec.Base.consumed_length b)
| _ -> FStar.Pervasives.Native.None)
<:
FStar.Pervasives.Native.option (FStar.UInt32.t *
LowParse.Spec.Base.consumed_length b))
<:
FStar.Pervasives.Native.option (FStar.UInt32.t * LowParse.Spec.Base.consumed_length b)
)
<:
FStar.Pervasives.Native.option (FStar.UInt32.t * LowParse.Spec.Base.consumed_length b))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.parse",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.BoundedInt.parse_bounded_integer_le",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Combinators.parse_filter_eq",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt32.v",
"Prims.bool",
"Prims.unit",
"LowParse.Spec.Combinators.parse_synth_eq",
"LowParse.Spec.Combinators.parse_filter_kind",
"LowParse.Spec.Combinators.parse_filter_refine",
"FStar.UInt32.t",
"LowParse.Spec.Combinators.parse_filter",
"FStar.Seq.Base.seq",
"LowParse.Bytes.byte",
"FStar.Seq.Base.slice",
"FStar.Seq.Base.length",
"LowParse.Spec.Combinators.and_then_eq",
"LowParse.Spec.BCVLI.parse_bcvli_payload_kind",
"LowParse.Spec.BCVLI.parse_bcvli_payload"
] | [] | false | false | true | false | false | let parse_bcvli_eq b =
| and_then_eq (parse_bounded_integer_le 1) parse_bcvli_payload b;
match parse (parse_bounded_integer_le 1) b with
| None -> ()
| Some (x32, consumed_x) ->
let b' = Seq.slice b consumed_x (Seq.length b) in
parse_synth_eq ((parse_bounded_integer_le 2) `parse_filter` (fun x -> U32.v x >= 253))
(fun x -> (x <: U32.t))
b';
parse_filter_eq (parse_bounded_integer_le 2) (fun x -> U32.v x >= 253) b';
parse_synth_eq ((parse_bounded_integer_le 4) `parse_filter` (fun x -> U32.v x >= 65536))
(fun x -> (x <: U32.t))
b';
parse_filter_eq (parse_bounded_integer_le 4) (fun x -> U32.v x >= 65536) b' | false |
STLC.Core.fst | STLC.Core.check | val check
(g: R.env)
(sg: list (var & stlc_ty))
(e: stlc_exp{ln e /\ ((freevars e) `Set.subset` (vars_of_env sg))})
: T.Tac (t: stlc_ty & stlc_typing sg e t) | val check
(g: R.env)
(sg: list (var & stlc_ty))
(e: stlc_exp{ln e /\ ((freevars e) `Set.subset` (vars_of_env sg))})
: T.Tac (t: stlc_ty & stlc_typing sg e t) | let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1)) | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 49,
"end_line": 279,
"start_col": 0,
"start_line": 238
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 |
g: FStar.Stubs.Reflection.Types.env ->
sg: Prims.list (STLC.Core.var * STLC.Core.stlc_ty) ->
e:
STLC.Core.stlc_exp
{STLC.Core.ln e /\ FStar.Set.subset (STLC.Core.freevars e) (STLC.Core.vars_of_env sg)}
-> FStar.Tactics.Effect.Tac
(Prims.dtuple2 STLC.Core.stlc_ty (fun t -> STLC.Core.stlc_typing sg e t)) | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Stubs.Reflection.Types.env",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"STLC.Core.var",
"STLC.Core.stlc_ty",
"STLC.Core.stlc_exp",
"Prims.l_and",
"Prims.b2t",
"STLC.Core.ln",
"FStar.Set.subset",
"STLC.Core.freevars",
"STLC.Core.vars_of_env",
"Prims.Mkdtuple2",
"STLC.Core.stlc_typing",
"STLC.Core.TUnit",
"STLC.Core.EUnit",
"STLC.Core.T_Unit",
"Prims.dtuple2",
"STLC.Core.lookup",
"FStar.Tactics.V2.Derived.fail",
"STLC.Core.EVar",
"FStar.Pervasives.Native.__proj__Some__item__v",
"STLC.Core.T_Var",
"Prims.Cons",
"FStar.Pervasives.Native.Mktuple2",
"STLC.Core.open_exp",
"STLC.Core.TArrow",
"STLC.Core.T_Lam",
"STLC.Core.check",
"Prims.unit",
"STLC.Core.freevars_open",
"STLC.Core.fresh_is_fresh",
"STLC.Core.fresh",
"Prims.op_Equality",
"STLC.Core.T_App",
"Prims.bool",
"FStar.Printf.sprintf",
"STLC.Core.ty_to_string"
] | [
"recursion"
] | false | true | false | false | false | let rec check
(g: R.env)
(sg: list (var & stlc_ty))
(e: stlc_exp{ln e /\ ((freevars e) `Set.subset` (vars_of_env sg))})
: T.Tac (t: stlc_ty & stlc_typing sg e t) =
| match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
(match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |))
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody , dbody |) = check g ((x, t) :: sg) (open_exp e x) in
(| TArrow t tbody, T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1 , d1 |) = check g sg e1 in
let (| t2 , d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else
T.fail (Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ -> T.fail (Printf.sprintf "Expected an arrow, got %s" (ty_to_string t1)) | false |
STLC.Core.fst | STLC.Core.elab_exp_freevars | val elab_exp_freevars (e: stlc_exp) : Lemma ((freevars e) `Set.equal` (RT.freevars (elab_exp e))) | val elab_exp_freevars (e: stlc_exp) : Lemma ((freevars e) `Set.equal` (RT.freevars (elab_exp e))) | let rec elab_exp_freevars (e:stlc_exp)
: Lemma (freevars e `Set.equal` RT.freevars (elab_exp e))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam t e ->
elab_ty_freevars t;
elab_exp_freevars e
| EApp e1 e2 ->
elab_exp_freevars e1;
elab_exp_freevars e2 | {
"file_name": "examples/dsls/stlc/STLC.Core.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 26,
"end_line": 459,
"start_col": 0,
"start_line": 448
} | module STLC.Core
module T = FStar.Tactics.V2
module R = FStar.Reflection.V2
module L = FStar.List.Tot
module RT = FStar.Reflection.Typing
type stlc_ty =
| TUnit
| TArrow : stlc_ty -> stlc_ty -> stlc_ty
let var = nat
let index = nat
noeq
type stlc_exp =
| EUnit : stlc_exp
| EBVar : index -> stlc_exp
| EVar : var -> stlc_exp
| ELam : stlc_ty -> stlc_exp -> stlc_exp
| EApp : stlc_exp -> stlc_exp -> stlc_exp
let rec size (e:stlc_exp)
: nat
= match e with
| EUnit
| EBVar _
| EVar _ -> 1
| ELam _ e -> 1 + size e
| EApp e1 e2 -> 1 + size e1 + size e2
let rec ln' (e:stlc_exp) (n:int)
: bool
= match e with
| EUnit
| EVar _ -> true
| EBVar m -> m <= n
| ELam _ e -> ln' e (n + 1)
| EApp e1 e2 -> ln' e1 n && ln' e2 n
let ln e = ln' e (-1)
let rec open_exp' (e:stlc_exp) (v:var) (n:index)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> EVar m
| EBVar m -> if m = n then EVar v else EBVar m
| ELam t e -> ELam t (open_exp' e v (n + 1))
| EApp e1 e2 -> EApp (open_exp' e1 v n) (open_exp' e2 v n)
let rec close_exp' (e:stlc_exp) (v:var) (n:nat)
: e':stlc_exp { size e == size e'}
= match e with
| EUnit -> EUnit
| EVar m -> if m = v then EBVar n else EVar m
| EBVar m -> EBVar m
| ELam t e -> ELam t (close_exp' e v (n + 1))
| EApp e1 e2 -> EApp (close_exp' e1 v n) (close_exp' e2 v n)
let open_exp e v = open_exp' e v 0
let close_exp e v = close_exp' e v 0
let rec open_close' (e:stlc_exp) (x:var) (n:nat { ln' e (n - 1) })
: Lemma (open_exp' (close_exp' e x n) x n == e)
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_close' e x (n + 1)
| EApp e1 e2 ->
open_close' e1 x n;
open_close' e2 x n
let open_close (e:stlc_exp) (x:var)
: Lemma
(requires ln e)
(ensures open_exp (close_exp e x) x == e)
[SMTPat (open_exp (close_exp e x) x)]
= open_close' e x 0
let rec open_exp_ln (e:stlc_exp) (v:var) (n:index) (m:int)
: Lemma
(requires ln' e n /\
m == n - 1)
(ensures ln' (open_exp' e v n) m)
[SMTPat (ln' e n);
SMTPat (ln' (open_exp' e v n) m)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> open_exp_ln e v (n + 1) (m + 1)
| EApp e1 e2 -> open_exp_ln e1 v n m; open_exp_ln e2 v n m
let rec close_exp_ln (e:stlc_exp) (v:var) (n:nat)
: Lemma
(requires ln' e (n - 1))
(ensures ln' (close_exp' e v n) n)
[SMTPat (ln' (close_exp' e v n) n)]
= match e with
| EUnit -> ()
| EVar _ -> ()
| EBVar m -> ()
| ELam _ e -> close_exp_ln e v (n + 1)
| EApp e1 e2 -> close_exp_ln e1 v n; close_exp_ln e2 v n
let rec freevars (e:stlc_exp)
: Set.set var
= match e with
| EUnit
| EBVar _ -> Set.empty
| EVar x -> Set.singleton x
| ELam _ e -> freevars e
| EApp e1 e2 -> freevars e1 `Set.union` freevars e2
let rec closed (e:stlc_exp)
: b:bool { b <==> (freevars e `Set.equal` Set.empty) }
= match e with
| EUnit
| EBVar _ -> true
| EVar x ->
assert (x `Set.mem` freevars e);
false
| ELam _ e -> closed e
| EApp e1 e2 -> closed e1 && closed e2
let rec freevars_open (e:stlc_exp) (x:var) (n:nat)
: Lemma (freevars (open_exp' e x n) `Set.subset`
(freevars e `Set.union` Set.singleton x))
= match e with
| EUnit
| EBVar _
| EVar _ -> ()
| ELam _ e -> freevars_open e x (n + 1)
| EApp e1 e2 ->
freevars_open e1 x n;
freevars_open e2 x n
let stlc_env = list (var & stlc_ty)
let lookup (e:list (var & 'a)) (x:var)
: option 'a
= L.assoc x e
let max (n1 n2:nat) = if n1 < n2 then n2 else n1
let rec fresh (e:list (var & 'a))
: var
= match e with
| [] -> 0
| hd :: tl ->
max (fresh tl) (fst hd) + 1
let _ = assert (fresh [1,();2,();3,();4,()] == 8) // odd.. but ok
let rec fresh_not_mem (e:list (var & 'a)) (elt: (var & 'a))
: Lemma (ensures L.memP elt e ==> fresh e > fst elt)
= match e with
| [] -> ()
| hd :: tl -> fresh_not_mem tl elt
let lookup_mem (e:list (var & 'a)) (x:var)
: Lemma
(requires Some? (lookup e x))
(ensures exists elt. L.memP elt e /\ fst elt == x)
= let Some y = lookup e x in
List.Tot.Properties.assoc_memP_some x y e
let fresh_is_fresh (e:list (var & 'a))
: Lemma (None? (lookup e (fresh e)))
= match lookup e (fresh e) with
| None -> ()
| Some _ ->
lookup_mem e (fresh e);
FStar.Classical.forall_intro (fresh_not_mem e)
[@@erasable]
noeq
type stlc_typing : stlc_env -> stlc_exp -> stlc_ty -> Type =
| T_Unit :
g:stlc_env ->
stlc_typing g EUnit TUnit
| T_Var :
g:stlc_env ->
x:var { Some? (lookup g x) } ->
stlc_typing g (EVar x) (Some?.v (lookup g x))
| T_Lam :
g:stlc_env ->
t:stlc_ty ->
e:stlc_exp ->
t':stlc_ty ->
x:var { None? (lookup g x) /\ ~ (Set.mem x (freevars e)) } ->
stlc_typing ((x,t)::g) (open_exp e x) t' ->
stlc_typing g (ELam t e) (TArrow t t')
| T_App :
g:stlc_env ->
e1:stlc_exp ->
e2:stlc_exp ->
t:stlc_ty ->
t':stlc_ty ->
stlc_typing g e1 (TArrow t t') ->
stlc_typing g e2 t ->
stlc_typing g (EApp e1 e2) t'
let tun = R.pack_ln R.Tv_Unknown
let rec ty_to_string' should_paren (t:stlc_ty)
: Tot string (decreases t)
= match t with
| TUnit -> "unit"
| TArrow t1 t2 ->
let t = Printf.sprintf "%s -> %s"
(ty_to_string' true t1)
(ty_to_string' false t2) in
if should_paren
then Printf.sprintf "(%s)" t
else t
let ty_to_string = ty_to_string' false
let mem_intension_pat (#a:eqtype) (x:a) (f:(a -> Tot bool))
: Lemma
(ensures FStar.Set.(mem x (intension f) = f x))
[SMTPat FStar.Set.(mem x (intension f))]
= Set.mem_intension x f
let contains (sg:list (var & stlc_ty)) (x:var) =
Some? (lookup sg x)
let vars_of_env (sg:list (var & stlc_ty))
: GTot (Set.set var)
= Set.intension (contains sg)
let rec check (g:R.env)
(sg:list (var & stlc_ty))
(e:stlc_exp { ln e /\ (freevars e `Set.subset` vars_of_env sg)})
: T.Tac (t:stlc_ty &
stlc_typing sg e t)
= match e with
| EUnit ->
let d = T_Unit sg in
(| TUnit, d |)
| EVar n ->
begin
match lookup sg n with
| None -> T.fail "Ill-typed"
| Some t ->
let d = T_Var sg n in
(| t, d |)
end
| ELam t e ->
let x = fresh sg in
fresh_is_fresh sg;
freevars_open e x 0;
let (| tbody, dbody |) = check g ((x,t)::sg) (open_exp e x) in
(| TArrow t tbody,
T_Lam sg t e tbody x dbody |)
| EApp e1 e2 ->
let (| t1, d1 |) = check g sg e1 in
let (| t2, d2 |) = check g sg e2 in
match t1 with
| TArrow t2' t ->
if t2' = t2
then (| t, T_App _ _ _ _ _ d1 d2 |)
else T.fail
(Printf.sprintf "Expected argument of type %s got %s"
(ty_to_string t2')
(ty_to_string t2))
| _ ->
T.fail (Printf.sprintf "Expected an arrow, got %s"
(ty_to_string t1))
let rec elab_ty (t:stlc_ty)
: R.term
= let open R in
match t with
| TUnit ->
R.pack_ln (R.Tv_FVar (R.pack_fv unit_lid))
| TArrow t1 t2 ->
let t1 = elab_ty t1 in
let t2 = elab_ty t2 in
R.pack_ln
(R.Tv_Arrow
(RT.mk_simple_binder RT.pp_name_default t1)
(R.pack_comp (C_Total t2)))
let rec elab_exp (e:stlc_exp)
: Tot R.term (decreases (size e))
= let open R in
match e with
| EUnit ->
pack_ln (Tv_Const C_Unit)
| EBVar n ->
let bv = R.pack_bv (RT.make_bv n) in
R.pack_ln (Tv_BVar bv)
| EVar n ->
let bv = R.pack_namedv (RT.make_namedv n) in
R.pack_ln (Tv_Var bv)
| ELam t e ->
let t = elab_ty t in
let e = elab_exp e in
R.pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default t) e)
| EApp e1 e2 ->
let e1 = elab_exp e1 in
let e2 = elab_exp e2 in
R.pack_ln (Tv_App e1 (e2, Q_Explicit))
let extend_env_l (g:R.env) (sg:stlc_env) =
L.fold_right (fun (x, t) g -> RT.extend_env g x (elab_ty t)) sg g
let rec extend_env_l_lookup_bvar (g:R.env) (sg:stlc_env) (x:var)
: Lemma
(requires (forall x. RT.lookup_bvar g x == None))
(ensures (
match lookup sg x with
| Some t -> RT.lookup_bvar (extend_env_l g sg) x == Some (elab_ty t)
| None -> RT.lookup_bvar (extend_env_l g sg) x == None))
(decreases sg)
[SMTPat (RT.lookup_bvar (extend_env_l g sg) x)]
= match sg with
| [] -> ()
| hd :: tl -> extend_env_l_lookup_bvar g tl x
open FStar.Calc
//key lemma about STLC types: Their elaborations are closed
let rec stlc_types_are_closed_core (ty:stlc_ty) (ss:RT.subst)
: Lemma (ensures RT.subst_term (elab_ty ty) ss == elab_ty ty)
(decreases ty)
[SMTPat (RT.subst_term (elab_ty ty) ss)]
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
stlc_types_are_closed_core t1 ss;
stlc_types_are_closed_core t2 (RT.shift_subst ss)
let stlc_types_are_closed1 (ty:stlc_ty) (v:R.term)
: Lemma (RT.open_with (elab_ty ty) v == elab_ty ty)
[SMTPat (RT.open_with (elab_ty ty) v)]
= stlc_types_are_closed_core ty [ RT.DT 0 v ];
RT.open_with_spec (elab_ty ty) v
let stlc_types_are_closed2 (ty:stlc_ty) (x:R.var)
: Lemma (RT.close_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.close_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.ND x 0 ];
RT.close_term_spec (elab_ty ty) x
let stlc_types_are_closed3 (ty:stlc_ty) (x:R.var)
: Lemma (RT.open_term (elab_ty ty) x == elab_ty ty)
[SMTPat (RT.open_term (elab_ty ty) x)]
= stlc_types_are_closed_core ty [ RT.DT 0 (RT.var_as_term x) ];
RT.open_term_spec (elab_ty ty) x
let rec elab_ty_freevars (ty:stlc_ty)
: Lemma (RT.freevars (elab_ty ty) `Set.equal` Set.empty)
= match ty with
| TUnit -> ()
| TArrow t1 t2 ->
elab_ty_freevars t1;
elab_ty_freevars t2
let rec elab_open_commute' (e:stlc_exp) (x:var) (n:nat)
: Lemma (ensures
RT.subst_term (elab_exp e) (RT.open_with_var x n) ==
elab_exp (open_exp' e x n))
(decreases e)
= match e with
| EUnit -> ()
| EBVar _ -> ()
| EVar _ -> ()
| EApp e1 e2 ->
elab_open_commute' e1 x n;
elab_open_commute' e2 x n
| ELam t e ->
calc (==) {
elab_exp (open_exp' (ELam t e) x n);
(==) {}
elab_exp (ELam t (open_exp' e x (n + 1)));
(==) { }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp (open_exp' e x (n + 1)))));
(==) { elab_open_commute' e x (n + 1) }
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t))
(RT.subst_term (elab_exp e) RT.(open_with_var x (n + 1)))));
(==) { stlc_types_are_closed_core t (RT.open_with_var x n) }
RT.subst_term
R.(pack_ln (Tv_Abs (RT.mk_simple_binder RT.pp_name_default (elab_ty t)) (elab_exp e)))
RT.(open_with_var x n);
}
let elab_open_commute (e:stlc_exp) (x:var)
: Lemma (RT.open_term (elab_exp e) x == elab_exp (open_exp e x))
[SMTPat (RT.open_term (elab_exp e) x)]
= elab_open_commute' e x 0;
RT.open_term_spec (elab_exp e) x
let rec extend_env_l_lookup_fvar (g:R.env) (sg:stlc_env) (fv:R.fv)
: Lemma
(ensures
RT.lookup_fvar (extend_env_l g sg) fv ==
RT.lookup_fvar g fv)
[SMTPat (RT.lookup_fvar (extend_env_l g sg) fv)]
= match sg with
| [] -> ()
| hd::tl -> extend_env_l_lookup_fvar g tl fv
let rec elab_ty_soundness (g:RT.fstar_top_env)
(sg:stlc_env)
(t:stlc_ty)
: GTot (RT.tot_typing (extend_env_l g sg) (elab_ty t) (RT.tm_type RT.u_zero))
(decreases t)
= match t with
| TUnit ->
RT.T_FVar _ RT.unit_fv
| TArrow t1 t2 ->
let t1_ok = elab_ty_soundness g sg t1 in
let x = fresh sg in
fresh_is_fresh sg;
elab_ty_freevars t2;
let t2_ok = elab_ty_soundness g ((x, t1)::sg) t2 in
let arr_max
: RT.tot_typing
(extend_env_l g sg)
(elab_ty t)
(RT.tm_type RT.(u_max u_zero u_zero))
= RT.T_Arrow _ x (elab_ty t1) (elab_ty t2)
_ _ RT.pp_name_default R.Q_Explicit T.E_Total _ _ t1_ok t2_ok
in
RT.simplify_umax arr_max | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.fst.checked",
"FStar.Reflection.Typing.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Properties.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "STLC.Core.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Reflection.Typing",
"short_module": "RT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "L"
},
{
"abbrev": true,
"full_module": "FStar.Reflection.V2",
"short_module": "R"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": false,
"full_module": "STLC",
"short_module": null
},
{
"abbrev": 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 | e: STLC.Core.stlc_exp
-> FStar.Pervasives.Lemma
(ensures
FStar.Set.equal (STLC.Core.freevars e)
(FStar.Reflection.Typing.freevars (STLC.Core.elab_exp e))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"STLC.Core.stlc_exp",
"STLC.Core.index",
"STLC.Core.var",
"STLC.Core.stlc_ty",
"STLC.Core.elab_exp_freevars",
"Prims.unit",
"STLC.Core.elab_ty_freevars",
"Prims.l_True",
"Prims.squash",
"FStar.Set.equal",
"STLC.Core.freevars",
"FStar.Reflection.Typing.freevars",
"STLC.Core.elab_exp",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec elab_exp_freevars (e: stlc_exp)
: Lemma ((freevars e) `Set.equal` (RT.freevars (elab_exp e))) =
| match e with
| EUnit | EBVar _ | EVar _ -> ()
| ELam t e ->
elab_ty_freevars t;
elab_exp_freevars e
| EApp e1 e2 ->
elab_exp_freevars e1;
elab_exp_freevars e2 | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.matrix_t | val matrix_t : n1: Lib.IntTypes.size_t ->
n2: Lib.IntTypes.size_t{Lib.IntTypes.v n1 * Lib.IntTypes.v n2 <= Lib.IntTypes.max_size_t}
-> Type0 | let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 25,
"end_line": 32,
"start_col": 0,
"start_line": 31
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
n1: Lib.IntTypes.size_t ->
n2: Lib.IntTypes.size_t{Lib.IntTypes.v n1 * Lib.IntTypes.v n2 <= Lib.IntTypes.max_size_t}
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Lib.Buffer.lbuffer",
"Hacl.Impl.Matrix.elem",
"Lib.IntTypes.op_Star_Bang"
] | [] | false | false | false | false | true | let matrix_t (n1: size_t) (n2: size_t{v n1 * v n2 <= max_size_t}) =
| lbuffer elem (n1 *! n2) | false |
|
LowParse.Spec.BCVLI.fst | LowParse.Spec.BCVLI.parse_bcvli_payload_and_then_cases_injective | val parse_bcvli_payload_and_then_cases_injective:squash (and_then_cases_injective parse_bcvli_payload
) | val parse_bcvli_payload_and_then_cases_injective:squash (and_then_cases_injective parse_bcvli_payload
) | let parse_bcvli_payload_and_then_cases_injective : squash (and_then_cases_injective parse_bcvli_payload) =
and_then_cases_injective_intro parse_bcvli_payload (fun x1 x2 b1 b2 ->
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) (fun x -> (x <: U32.t)) b2;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b1;
parse_synth_eq (parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) (fun x -> (x <: U32.t)) b2
) | {
"file_name": "src/lowparse/LowParse.Spec.BCVLI.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 29,
"start_col": 0,
"start_line": 23
} | module LowParse.Spec.BCVLI
open LowParse.Spec.Combinators // for parse_ret
module U32 = FStar.UInt32
module Seq = FStar.Seq
inline_for_extraction
let parse_bcvli_payload_kind = {
parser_kind_low = 0;
parser_kind_high = Some 4;
parser_kind_subkind = Some ParserStrong;
parser_kind_metadata = None;
}
let parse_bcvli_payload (x: bounded_integer 1) : Tot (parser parse_bcvli_payload_kind U32.t) =
if U32.v x <= 252 then weaken parse_bcvli_payload_kind (parse_ret (x <: U32.t)) else
if U32.v x = 253 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 2 `parse_filter` (fun x -> U32.v x >= 253)) `parse_synth` (fun x -> (x <: U32.t))) else
if U32.v x = 254 then weaken parse_bcvli_payload_kind ((parse_bounded_integer_le 4 `parse_filter` (fun x -> U32.v x >= 65536)) `parse_synth` (fun x -> (x <: U32.t))) else
fail_parser parse_bcvli_payload_kind U32.t
#push-options "--z3rlimit 32" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Combinators.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.Spec.BCVLI.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators // for parse_ret",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.squash (LowParse.Spec.Combinators.and_then_cases_injective LowParse.Spec.BCVLI.parse_bcvli_payload
) | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Combinators.and_then_cases_injective_intro",
"LowParse.Spec.BoundedInt.bounded_integer",
"FStar.UInt32.t",
"LowParse.Spec.BCVLI.parse_bcvli_payload",
"LowParse.Bytes.bytes",
"LowParse.Spec.Combinators.parse_synth_eq",
"LowParse.Spec.Combinators.parse_filter_kind",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.Combinators.parse_filter_refine",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt32.v",
"Prims.bool",
"LowParse.Spec.Combinators.parse_filter",
"LowParse.Spec.BoundedInt.parse_bounded_integer_le",
"Prims.unit"
] | [] | false | false | true | false | false | let parse_bcvli_payload_and_then_cases_injective:squash (and_then_cases_injective parse_bcvli_payload
) =
| and_then_cases_injective_intro parse_bcvli_payload
(fun x1 x2 b1 b2 ->
parse_synth_eq ((parse_bounded_integer_le 2) `parse_filter` (fun x -> U32.v x >= 253))
(fun x -> (x <: U32.t))
b1;
parse_synth_eq ((parse_bounded_integer_le 2) `parse_filter` (fun x -> U32.v x >= 253))
(fun x -> (x <: U32.t))
b2;
parse_synth_eq ((parse_bounded_integer_le 4) `parse_filter` (fun x -> U32.v x >= 65536))
(fun x -> (x <: U32.t))
b1;
parse_synth_eq ((parse_bounded_integer_le 4) `parse_filter` (fun x -> U32.v x >= 65536))
(fun x -> (x <: U32.t))
b2) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.matrix_create | val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2)) | val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2)) | let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 20,
"end_line": 52,
"start_col": 0,
"start_line": 49
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
n1: Lib.IntTypes.size_t ->
n2:
Lib.IntTypes.size_t
{ 0 < Lib.IntTypes.v n1 * Lib.IntTypes.v n2 /\
Lib.IntTypes.v n1 * Lib.IntTypes.v n2 <= Lib.IntTypes.max_size_t }
-> FStar.HyperStack.ST.StackInline (Hacl.Impl.Matrix.matrix_t n1 n2) | FStar.HyperStack.ST.StackInline | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.max_size_t",
"Lib.Buffer.create",
"Hacl.Impl.Matrix.elem",
"Lib.IntTypes.u16",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.int_t",
"Lib.IntTypes.size",
"FStar.Pervasives.normalize_term",
"Lib.IntTypes.size_nat",
"Hacl.Impl.Matrix.matrix_t"
] | [] | false | true | false | false | false | let matrix_create n1 n2 =
| [@@ inline_let ]let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.op_String_Assignment | val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x) | val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x) | let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 57,
"end_line": 103,
"start_col": 0,
"start_line": 103
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
m: Hacl.Impl.Matrix.matrix_t n1 n2 ->
ij:
(Lib.IntTypes.size_t * Lib.IntTypes.size_t)
{ let _ = ij in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ i j = _ in
Lib.IntTypes.v i < Lib.IntTypes.v n1 /\ Lib.IntTypes.v j < Lib.IntTypes.v n2)
<:
Type0 } ->
x: Hacl.Impl.Matrix.elem
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Hacl.Impl.Matrix.matrix_t",
"FStar.Pervasives.Native.tuple2",
"Prims.l_and",
"Prims.op_LessThan",
"Lib.IntTypes.int_t",
"Hacl.Impl.Matrix.elem",
"Hacl.Impl.Matrix.mset",
"Prims.unit"
] | [] | false | true | false | false | false | let ( .[]<- ) #n1 #n2 m (i, j) x =
| mset m i j x | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.op_String_Access | val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j)) | val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j)) | let op_String_Access #n1 #n2 m (i,j) = mget m i j | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 49,
"end_line": 95,
"start_col": 0,
"start_line": 95
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
m: Hacl.Impl.Matrix.matrix_t n1 n2 ->
ij:
(Lib.IntTypes.size_t * Lib.IntTypes.size_t)
{ let _ = ij in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ i j = _ in
Lib.IntTypes.v i < Lib.IntTypes.v n1 /\ Lib.IntTypes.v j < Lib.IntTypes.v n2)
<:
Type0 }
-> FStar.HyperStack.ST.Stack Hacl.Impl.Matrix.elem | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Hacl.Impl.Matrix.matrix_t",
"FStar.Pervasives.Native.tuple2",
"Prims.l_and",
"Prims.op_LessThan",
"Lib.IntTypes.int_t",
"Hacl.Impl.Matrix.mget",
"Hacl.Impl.Matrix.elem"
] | [] | false | true | false | false | false | let ( .[] ) #n1 #n2 m (i, j) =
| mget m i j | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.mset | val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x) | val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x) | let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 23,
"end_line": 87,
"start_col": 0,
"start_line": 85
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_t{Lib.IntTypes.v j < Lib.IntTypes.v n2} ->
x: Hacl.Impl.Matrix.elem
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Hacl.Impl.Matrix.matrix_t",
"Prims.op_LessThan",
"Hacl.Impl.Matrix.elem",
"Lib.Buffer.op_Array_Assignment",
"Lib.IntTypes.op_Star_Bang",
"Lib.IntTypes.op_Plus_Bang",
"Prims.unit",
"Spec.Matrix.index_lt"
] | [] | false | true | false | false | false | let mset #n1 #n2 a i j x =
| M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.get | val get : h: FStar.Monotonic.HyperStack.mem ->
m: Hacl.Impl.Matrix.matrix_t n1 n2 ->
i: Lib.IntTypes.size_nat{i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_nat{j < Lib.IntTypes.v n2}
-> Prims.GTot Spec.Matrix.elem | let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 69,
"end_line": 107,
"start_col": 0,
"start_line": 107
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h: FStar.Monotonic.HyperStack.mem ->
m: Hacl.Impl.Matrix.matrix_t n1 n2 ->
i: Lib.IntTypes.size_nat{i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_nat{j < Lib.IntTypes.v n2}
-> Prims.GTot Spec.Matrix.elem | Prims.GTot | [
"sometrivial"
] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Impl.Matrix.matrix_t",
"Lib.IntTypes.size_nat",
"Prims.op_LessThan",
"Spec.Matrix.mget",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.elem"
] | [] | false | false | false | false | false | let get #n1 #n2 h (m: matrix_t n1 n2) i j =
| M.mget (as_matrix h m) i j | false |
|
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.mget | val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j)) | val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j)) | let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 18,
"end_line": 69,
"start_col": 0,
"start_line": 67
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_t{Lib.IntTypes.v j < Lib.IntTypes.v n2}
-> FStar.HyperStack.ST.Stack Hacl.Impl.Matrix.elem | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Hacl.Impl.Matrix.matrix_t",
"Prims.op_LessThan",
"Lib.Buffer.op_Array_Access",
"Lib.Buffer.MUT",
"Hacl.Impl.Matrix.elem",
"Lib.IntTypes.op_Star_Bang",
"Lib.IntTypes.op_Plus_Bang",
"Prims.unit",
"Spec.Matrix.index_lt"
] | [] | false | true | false | false | false | let mget #n1 #n2 a i j =
| M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.map_inner_inv | val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0 | val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0 | let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 92,
"end_line": 130,
"start_col": 0,
"start_line": 123
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0 | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem ->
h2: FStar.Monotonic.HyperStack.mem ->
f: (_: Hacl.Impl.Matrix.elem -> Hacl.Impl.Matrix.elem) ->
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n1 n2 ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_nat
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Impl.Matrix.elem",
"Hacl.Impl.Matrix.matrix_t",
"Prims.op_LessThan",
"Lib.IntTypes.size_nat",
"Prims.l_and",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Lib.Buffer.modifies1",
"Prims.l_Forall",
"Prims.nat",
"Prims.eq2",
"Spec.Matrix.elem",
"Hacl.Impl.Matrix.get",
"Lib.IntTypes.sec_int_t",
"Lib.IntTypes.U16"
] | [] | false | false | false | false | true | let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
| live h2 a /\ live h2 c /\ modifies1 c h1 h2 /\ j <= v n2 /\
(forall (i0: nat{i0 < v i}) (j: nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0: nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0: nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0: nat{v i < i0 /\ i0 < v n1}) (j: nat{j < v n2}). get h2 c i0 j == get h0 c i0 j) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.mod_pow2_felem | val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a) | val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a) | let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 34,
"end_line": 193,
"start_col": 0,
"start_line": 192
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
logq: Lib.IntTypes.size_t{0 < Lib.IntTypes.v logq /\ Lib.IntTypes.v logq < 16} ->
a: Lib.IntTypes.uint16
-> Prims.Pure Lib.IntTypes.uint16 | Prims.Pure | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.uint16",
"Lib.IntTypes.op_Amp_Dot",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Lib.IntTypes.op_Subtraction_Dot",
"Lib.IntTypes.op_Less_Less_Dot",
"Lib.IntTypes.u16"
] | [] | false | false | false | false | false | let mod_pow2_felem logq a =
| a &. ((u16 1 <<. logq) -. u16 1) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.map2_inner_inv | val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0 | val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0 | let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 92,
"end_line": 238,
"start_col": 0,
"start_line": 231
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0 | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem ->
h2: FStar.Monotonic.HyperStack.mem ->
f: (_: Hacl.Impl.Matrix.elem -> _: Hacl.Impl.Matrix.elem -> Hacl.Impl.Matrix.elem) ->
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n1 n2 ->
c: Hacl.Impl.Matrix.matrix_t n1 n2 ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_nat
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Impl.Matrix.elem",
"Hacl.Impl.Matrix.matrix_t",
"Prims.op_LessThan",
"Lib.IntTypes.size_nat",
"Prims.l_and",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Lib.Buffer.modifies1",
"Prims.l_Forall",
"Prims.nat",
"Prims.eq2",
"Spec.Matrix.elem",
"Hacl.Impl.Matrix.get",
"Lib.IntTypes.sec_int_t",
"Lib.IntTypes.U16"
] | [] | false | false | false | false | true | let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
| live h2 a /\ live h2 b /\ live h2 c /\ modifies1 c h1 h2 /\ j <= v n2 /\
(forall (i0: nat{i0 < v i}) (j: nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0: nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0: nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0: nat{v i < i0 /\ i0 < v n1}) (j: nat{j < v n2}). get h2 c i0 j == get h0 c i0 j) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.matrix_add | val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b)) | val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b)) | let matrix_add #n1 #n2 a b =
map2 add_mod a b a | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 20,
"end_line": 312,
"start_col": 0,
"start_line": 311
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Impl.Matrix.matrix_t n1 n2 -> b: Hacl.Impl.Matrix.matrix_t n1 n2
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Hacl.Impl.Matrix.matrix_t",
"Hacl.Impl.Matrix.map2",
"Lib.IntTypes.add_mod",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Prims.unit"
] | [] | false | true | false | false | false | let matrix_add #n1 #n2 a b =
| map2 add_mod a b a | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.mod_pow2 | val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a)) | val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a)) | let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 78,
"end_line": 213,
"start_col": 0,
"start_line": 207
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
logq: Lib.IntTypes.size_t{0 < Lib.IntTypes.v logq /\ Lib.IntTypes.v logq <= 16} ->
a: Hacl.Impl.Matrix.matrix_t n1 n2
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Prims.l_and",
"Prims.op_LessThan",
"Hacl.Impl.Matrix.matrix_t",
"Lib.IntTypes.op_Less_Dot",
"FStar.UInt32.__uint_to_t",
"Spec.Matrix.extensionality",
"Spec.Matrix.map",
"Hacl.Impl.Matrix.mod_pow2_felem",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.mod_pow2_felem",
"Prims.unit",
"Hacl.Impl.Matrix.map",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Prims.bool"
] | [] | false | true | false | false | false | let mod_pow2 #n1 #n2 logq a =
| if logq <. 16ul
then
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality (M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.map_inner | val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1)) | val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1)) | let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j] | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 22,
"end_line": 149,
"start_col": 0,
"start_line": 148
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem ->
f: (_: Hacl.Impl.Matrix.elem -> Hacl.Impl.Matrix.elem) ->
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
c: Hacl.Impl.Matrix.matrix_t n1 n2 {a == c} ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_t{Lib.IntTypes.v j < Lib.IntTypes.v n2}
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Impl.Matrix.elem",
"Hacl.Impl.Matrix.matrix_t",
"Prims.eq2",
"Prims.op_LessThan",
"Hacl.Impl.Matrix.op_String_Assignment",
"FStar.Pervasives.Native.Mktuple2",
"Prims.unit",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Hacl.Impl.Matrix.op_String_Access"
] | [] | false | true | false | false | false | let map_inner #n1 #n2 h0 h1 f a c i j =
| c.[ i, j ] <- f a.[ i, j ] | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.get_s | val get_s : h: FStar.Monotonic.HyperStack.mem ->
m: Hacl.Impl.Matrix.matrix_t n1 n2 ->
i: Lib.IntTypes.size_nat{i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_nat{j < Lib.IntTypes.v n2}
-> Prims.GTot Spec.Matrix.elem | let get_s #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget_s (as_matrix h m) i j | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 73,
"end_line": 511,
"start_col": 0,
"start_line": 511
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0
let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b
inline_for_extraction noextract private
val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[i,k] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
private
val onemore: p:(nat -> Type0) -> q:(i:nat{p i} -> Type0) -> b:nat{p b} -> Lemma
(requires (forall (i:nat{p i /\ i < b}). q i) /\ q b)
(ensures forall (i:nat{p i /\ i <= b}). q i)
let onemore p q b = ()
val onemore1:
#n1:size_nat
-> #n3:size_nat{n1 * n3 <= max_size_t}
-> c:M.matrix n1 n3
-> f:(i:nat{i < n1} -> k:nat{k < n3} -> GTot uint16)
-> i:size_nat{i < n1} -> Lemma
(requires ((forall (i1:nat{i1 < i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k) /\ (forall (k:nat{k < n3}). M.mget c i k == f i k)))
(ensures (forall (i1:nat{i1 <= i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k))
let onemore1 #n1 #n3 c f i = ()
val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1 h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall (k:nat{k < v n3}). get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul (as_matrix h0 a) (as_matrix h0 b))
(* Special case of matrix multiplication *)
(* when we have a different way of accessing to entries of the matrix S *)
inline_for_extraction noextract
val mget_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies0 h0 h1 /\
x == M.mget_s (as_matrix h0 a) (v i) (v j))
let mget_s #n1 #n2 a i j =
M.index_lt (v n2) (v n1) (v j) (v i);
a.(j *! n1 +! i) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h: FStar.Monotonic.HyperStack.mem ->
m: Hacl.Impl.Matrix.matrix_t n1 n2 ->
i: Lib.IntTypes.size_nat{i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_nat{j < Lib.IntTypes.v n2}
-> Prims.GTot Spec.Matrix.elem | Prims.GTot | [
"sometrivial"
] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Impl.Matrix.matrix_t",
"Lib.IntTypes.size_nat",
"Prims.op_LessThan",
"Spec.Matrix.mget_s",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.elem"
] | [] | false | false | false | false | false | let get_s #n1 #n2 h (m: matrix_t n1 n2) i j =
| M.mget_s (as_matrix h m) i j | false |
|
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.map | val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a)) | val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a)) | let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a)) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 64,
"end_line": 181,
"start_col": 0,
"start_line": 165
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
f: (_: Lib.IntTypes.uint16 -> Lib.IntTypes.uint16) ->
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
c: Hacl.Impl.Matrix.matrix_t n1 n2
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Lib.IntTypes.uint16",
"Hacl.Impl.Matrix.matrix_t",
"Spec.Matrix.extensionality",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.map",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Lib.Loops.for",
"Lib.IntTypes.size",
"Prims.nat",
"Prims.l_and",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Hacl.Impl.Matrix.elem",
"Lib.Buffer.modifies1",
"Prims.l_Forall",
"Prims.op_LessThan",
"Prims.eq2",
"Hacl.Impl.Matrix.get",
"Spec.Matrix.elem",
"Hacl.Impl.Matrix.map_inner_inv",
"Hacl.Impl.Matrix.map_inner"
] | [] | false | true | false | false | false | let map #n1 #n2 f a c =
| let h0 = ST.get () in
Lib.Loops.for (size 0)
n1
(fun h1 i ->
live h1 a /\ live h1 c /\ modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0: nat{i0 < i}) (j: nat{j < v n2}). get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0: nat{i <= i0 /\ i0 < v n1}) (j: nat{j < v n2}). get h1 c i0 j == get h0 c i0 j))
(fun i ->
let h1 = ST.get () in
Lib.Loops.for (size 0)
n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j));
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a)) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.matrix_sub | val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b)) | val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b)) | let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b)) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 77,
"end_line": 334,
"start_col": 0,
"start_line": 326
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Impl.Matrix.matrix_t n1 n2 -> b: Hacl.Impl.Matrix.matrix_t n1 n2
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Hacl.Impl.Matrix.matrix_t",
"Spec.Matrix.extensionality",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.sub",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Impl.Matrix.map2",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Lib.IntTypes.sub_mod"
] | [] | false | true | false | false | false | let matrix_sub #n1 #n2 a b =
| let h0 = ST.get () in
[@@ inline_let ]let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get () in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b)) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.mul_inner_inv | val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0 | val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0 | let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 34,
"end_line": 403,
"start_col": 0,
"start_line": 394
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0 | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem ->
h2: FStar.Monotonic.HyperStack.mem ->
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n2 n3 ->
c: Hacl.Impl.Matrix.matrix_t n1 n3 ->
f: (k: Prims.nat{k < Lib.IntTypes.v n3} -> Prims.GTot Lib.IntTypes.uint16) ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
k: Lib.IntTypes.size_nat
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Prims.l_and",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Impl.Matrix.matrix_t",
"Prims.nat",
"Prims.op_LessThan",
"Lib.IntTypes.uint16",
"Lib.IntTypes.size_nat",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Hacl.Impl.Matrix.elem",
"Lib.Buffer.modifies1",
"Prims.l_Forall",
"Prims.eq2",
"Spec.Matrix.elem",
"Hacl.Impl.Matrix.get",
"Spec.Matrix.matrix",
"Hacl.Impl.Matrix.as_matrix"
] | [] | false | false | false | false | true | let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
| live h2 a /\ live h2 b /\ live h2 c /\ modifies1 c h1 h2 /\ k <= v n3 /\
(forall (i1: nat{i1 < v i}) (k: nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1: nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1: nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1: nat{v i < i1 /\ i1 < v n1}) (k: nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\ as_matrix h0 b == as_matrix h2 b | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.mget_s | val mget_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies0 h0 h1 /\
x == M.mget_s (as_matrix h0 a) (v i) (v j)) | val mget_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies0 h0 h1 /\
x == M.mget_s (as_matrix h0 a) (v i) (v j)) | let mget_s #n1 #n2 a i j =
M.index_lt (v n2) (v n1) (v j) (v i);
a.(j *! n1 +! i) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 18,
"end_line": 508,
"start_col": 0,
"start_line": 506
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0
let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b
inline_for_extraction noextract private
val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[i,k] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
private
val onemore: p:(nat -> Type0) -> q:(i:nat{p i} -> Type0) -> b:nat{p b} -> Lemma
(requires (forall (i:nat{p i /\ i < b}). q i) /\ q b)
(ensures forall (i:nat{p i /\ i <= b}). q i)
let onemore p q b = ()
val onemore1:
#n1:size_nat
-> #n3:size_nat{n1 * n3 <= max_size_t}
-> c:M.matrix n1 n3
-> f:(i:nat{i < n1} -> k:nat{k < n3} -> GTot uint16)
-> i:size_nat{i < n1} -> Lemma
(requires ((forall (i1:nat{i1 < i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k) /\ (forall (k:nat{k < n3}). M.mget c i k == f i k)))
(ensures (forall (i1:nat{i1 <= i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k))
let onemore1 #n1 #n3 c f i = ()
val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1 h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall (k:nat{k < v n3}). get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul (as_matrix h0 a) (as_matrix h0 b))
(* Special case of matrix multiplication *)
(* when we have a different way of accessing to entries of the matrix S *)
inline_for_extraction noextract
val mget_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies0 h0 h1 /\
x == M.mget_s (as_matrix h0 a) (v i) (v j)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_t{Lib.IntTypes.v j < Lib.IntTypes.v n2}
-> FStar.HyperStack.ST.Stack Hacl.Impl.Matrix.elem | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Hacl.Impl.Matrix.matrix_t",
"Prims.op_LessThan",
"Lib.Buffer.op_Array_Access",
"Lib.Buffer.MUT",
"Hacl.Impl.Matrix.elem",
"Lib.IntTypes.op_Star_Bang",
"Lib.IntTypes.op_Plus_Bang",
"Prims.unit",
"Spec.Matrix.index_lt"
] | [] | false | true | false | false | false | let mget_s #n1 #n2 a i j =
| M.index_lt (v n2) (v n1) (v j) (v i);
a.(j *! n1 +! i) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.map2_inner | val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1)) | val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1)) | let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j] | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 30,
"end_line": 258,
"start_col": 0,
"start_line": 257
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem ->
f: (_: Hacl.Impl.Matrix.elem -> _: Hacl.Impl.Matrix.elem -> Hacl.Impl.Matrix.elem) ->
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n1 n2 ->
c: Hacl.Impl.Matrix.matrix_t n1 n2 {a == c /\ Lib.Buffer.disjoint b c} ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
j: Lib.IntTypes.size_t{Lib.IntTypes.v j < Lib.IntTypes.v n2}
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Impl.Matrix.elem",
"Hacl.Impl.Matrix.matrix_t",
"Prims.l_and",
"Prims.eq2",
"Lib.Buffer.disjoint",
"Lib.Buffer.MUT",
"Prims.op_LessThan",
"Hacl.Impl.Matrix.op_String_Assignment",
"FStar.Pervasives.Native.Mktuple2",
"Prims.unit",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Hacl.Impl.Matrix.op_String_Access"
] | [] | false | true | false | false | false | let map2_inner #n1 #n2 h0 h1 f a b c i j =
| c.[ i, j ] <- f a.[ i, j ] b.[ i, j ] | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.map2 | val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b)) | val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b)) | let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b)) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 82,
"end_line": 297,
"start_col": 0,
"start_line": 281
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
f: (_: Lib.IntTypes.uint16 -> _: Lib.IntTypes.uint16 -> Lib.IntTypes.uint16) ->
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n1 n2 ->
c: Hacl.Impl.Matrix.matrix_t n1 n2
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Lib.IntTypes.uint16",
"Hacl.Impl.Matrix.matrix_t",
"Spec.Matrix.extensionality",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.map2",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Lib.Loops.for",
"Lib.IntTypes.size",
"Prims.nat",
"Prims.l_and",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Hacl.Impl.Matrix.elem",
"Lib.Buffer.modifies1",
"Prims.l_Forall",
"Prims.op_LessThan",
"Prims.eq2",
"Hacl.Impl.Matrix.get",
"Spec.Matrix.elem",
"Hacl.Impl.Matrix.map2_inner_inv",
"Hacl.Impl.Matrix.map2_inner"
] | [] | false | true | false | false | false | let map2 #n1 #n2 f a b c =
| let h0 = ST.get () in
Lib.Loops.for (size 0)
n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\ modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0: nat{i0 < i}) (j: nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0: nat{i <= i0 /\ i0 < v n1}) (j: nat{j < v n2}). get h1 c i0 j == get h0 c i0 j))
(fun i ->
let h1 = ST.get () in
Lib.Loops.for (size 0)
n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j));
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b)) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.mul_inner1 | val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1)) | val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1)) | let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[i,k] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k)) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 42,
"end_line": 429,
"start_col": 0,
"start_line": 424
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0
let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b
inline_for_extraction noextract private
val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem ->
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n2 n3 ->
c: Hacl.Impl.Matrix.matrix_t n1 n3 {Lib.Buffer.disjoint a c /\ Lib.Buffer.disjoint b c} ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
k: Lib.IntTypes.size_t{Lib.IntTypes.v k < Lib.IntTypes.v n3} ->
f:
(k: Prims.nat{k < Lib.IntTypes.v n3}
-> Prims.GTot
(res:
Lib.IntTypes.uint16
{ res ==
Spec.Matrix.sum (fun l ->
Hacl.Impl.Matrix.get h0 a (Lib.IntTypes.v i) l *.
Hacl.Impl.Matrix.get h0 b l k) }))
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Prims.l_and",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Impl.Matrix.matrix_t",
"Lib.Buffer.disjoint",
"Lib.Buffer.MUT",
"Hacl.Impl.Matrix.elem",
"Prims.op_LessThan",
"Prims.nat",
"Lib.IntTypes.uint16",
"Prims.eq2",
"Spec.Matrix.sum",
"Lib.IntTypes.size_nat",
"Lib.IntTypes.op_Star_Dot",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Hacl.Impl.Matrix.get",
"Prims._assert",
"Prims.unit",
"FStar.HyperStack.ST.get",
"Hacl.Impl.Matrix.op_String_Assignment",
"FStar.Pervasives.Native.Mktuple2",
"Lib.IntTypes.int_t",
"Hacl.Impl.Matrix.mul_inner",
"Spec.Matrix.mul_inner",
"Hacl.Impl.Matrix.as_matrix"
] | [] | false | true | false | false | false | let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
| assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[ i, k ] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k)) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.mul_inner1_s | val mul_inner1_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1)) | val mul_inner1_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1)) | let mul_inner1_s #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)));
mset c i k (mul_inner_s a b i k);
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k)) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 42,
"end_line": 577,
"start_col": 0,
"start_line": 572
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0
let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b
inline_for_extraction noextract private
val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[i,k] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
private
val onemore: p:(nat -> Type0) -> q:(i:nat{p i} -> Type0) -> b:nat{p b} -> Lemma
(requires (forall (i:nat{p i /\ i < b}). q i) /\ q b)
(ensures forall (i:nat{p i /\ i <= b}). q i)
let onemore p q b = ()
val onemore1:
#n1:size_nat
-> #n3:size_nat{n1 * n3 <= max_size_t}
-> c:M.matrix n1 n3
-> f:(i:nat{i < n1} -> k:nat{k < n3} -> GTot uint16)
-> i:size_nat{i < n1} -> Lemma
(requires ((forall (i1:nat{i1 < i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k) /\ (forall (k:nat{k < n3}). M.mget c i k == f i k)))
(ensures (forall (i1:nat{i1 <= i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k))
let onemore1 #n1 #n3 c f i = ()
val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1 h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall (k:nat{k < v n3}). get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul (as_matrix h0 a) (as_matrix h0 b))
(* Special case of matrix multiplication *)
(* when we have a different way of accessing to entries of the matrix S *)
inline_for_extraction noextract
val mget_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies0 h0 h1 /\
x == M.mget_s (as_matrix h0 a) (v i) (v j))
let mget_s #n1 #n2 a i j =
M.index_lt (v n2) (v n1) (v j) (v i);
a.(j *! n1 +! i)
unfold
let get_s #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget_s (as_matrix h m) i j
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner_s #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get_s h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = mget a i j in
let bjk = mget_s b j k in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)) (v n2);
assert (res == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
inline_for_extraction noextract private
val mul_inner1_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem ->
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n2 n3 ->
c: Hacl.Impl.Matrix.matrix_t n1 n3 {Lib.Buffer.disjoint a c /\ Lib.Buffer.disjoint b c} ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
k: Lib.IntTypes.size_t{Lib.IntTypes.v k < Lib.IntTypes.v n3} ->
f:
(k: Prims.nat{k < Lib.IntTypes.v n3}
-> Prims.GTot
(res:
Lib.IntTypes.uint16
{ res ==
Spec.Matrix.sum (fun l ->
Hacl.Impl.Matrix.get h0 a (Lib.IntTypes.v i) l *.
Hacl.Impl.Matrix.get_s h0 b l k) }))
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Prims.l_and",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Impl.Matrix.matrix_t",
"Lib.Buffer.disjoint",
"Lib.Buffer.MUT",
"Hacl.Impl.Matrix.elem",
"Prims.op_LessThan",
"Prims.nat",
"Lib.IntTypes.uint16",
"Prims.eq2",
"Spec.Matrix.sum",
"Lib.IntTypes.size_nat",
"Lib.IntTypes.op_Star_Dot",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Hacl.Impl.Matrix.get",
"Hacl.Impl.Matrix.get_s",
"Prims._assert",
"Prims.unit",
"FStar.HyperStack.ST.get",
"Hacl.Impl.Matrix.mset",
"Lib.IntTypes.int_t",
"Hacl.Impl.Matrix.mul_inner_s",
"Spec.Matrix.mul_inner_s",
"Hacl.Impl.Matrix.as_matrix"
] | [] | false | true | false | false | false | let mul_inner1_s #n1 #n2 #n3 h0 h1 a b c i k f =
| assert (M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)));
mset c i k (mul_inner_s a b i k);
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k)) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.matrix_eq | val matrix_eq:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.matrix_eq #(v n1) #(v n2) (as_matrix h0 a) (as_matrix h0 b)) | val matrix_eq:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.matrix_eq #(v n1) #(v n2) (as_matrix h0 a) (as_matrix h0 b)) | let matrix_eq #n1 #n2 a b =
push_frame();
let res = create 1ul (ones U16 SEC) in
let r = buf_eq_mask a b (n1 *! n2) res in
pop_frame ();
r | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 3,
"end_line": 638,
"start_col": 0,
"start_line": 633
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0
let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b
inline_for_extraction noextract private
val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[i,k] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
private
val onemore: p:(nat -> Type0) -> q:(i:nat{p i} -> Type0) -> b:nat{p b} -> Lemma
(requires (forall (i:nat{p i /\ i < b}). q i) /\ q b)
(ensures forall (i:nat{p i /\ i <= b}). q i)
let onemore p q b = ()
val onemore1:
#n1:size_nat
-> #n3:size_nat{n1 * n3 <= max_size_t}
-> c:M.matrix n1 n3
-> f:(i:nat{i < n1} -> k:nat{k < n3} -> GTot uint16)
-> i:size_nat{i < n1} -> Lemma
(requires ((forall (i1:nat{i1 < i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k) /\ (forall (k:nat{k < n3}). M.mget c i k == f i k)))
(ensures (forall (i1:nat{i1 <= i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k))
let onemore1 #n1 #n3 c f i = ()
val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1 h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall (k:nat{k < v n3}). get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul (as_matrix h0 a) (as_matrix h0 b))
(* Special case of matrix multiplication *)
(* when we have a different way of accessing to entries of the matrix S *)
inline_for_extraction noextract
val mget_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies0 h0 h1 /\
x == M.mget_s (as_matrix h0 a) (v i) (v j))
let mget_s #n1 #n2 a i j =
M.index_lt (v n2) (v n1) (v j) (v i);
a.(j *! n1 +! i)
unfold
let get_s #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget_s (as_matrix h m) i j
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner_s #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get_s h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = mget a i j in
let bjk = mget_s b j k in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)) (v n2);
assert (res == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
inline_for_extraction noextract private
val mul_inner1_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1_s #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)));
mset c i k (mul_inner_s a b i k);
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
val matrix_mul_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul_s (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul_s #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get_s h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get_s h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1_s h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall k. get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul_s (as_matrix h0 a) (as_matrix h0 b))
(* the end of the special matrix multiplication *)
val matrix_eq:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.matrix_eq #(v n1) #(v n2) (as_matrix h0 a) (as_matrix h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Impl.Matrix.matrix_t n1 n2 -> b: Hacl.Impl.Matrix.matrix_t n1 n2
-> FStar.HyperStack.ST.Stack Lib.IntTypes.uint16 | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Hacl.Impl.Matrix.matrix_t",
"Lib.IntTypes.uint16",
"Prims.unit",
"FStar.HyperStack.ST.pop_frame",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Lib.ByteBuffer.buf_eq_mask",
"Lib.IntTypes.op_Star_Bang",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Lib.Buffer.create",
"FStar.UInt32.__uint_to_t",
"Lib.IntTypes.ones",
"Lib.Buffer.lbuffer",
"FStar.HyperStack.ST.push_frame"
] | [] | false | true | false | false | false | let matrix_eq #n1 #n2 a b =
| push_frame ();
let res = create 1ul (ones U16 SEC) in
let r = buf_eq_mask a b (n1 *! n2) res in
pop_frame ();
r | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.matrix_from_lbytes | val matrix_from_lbytes:
#n1:size_t
-> #n2:size_t{2 * v n1 <= max_size_t /\ 2 * v n1 * v n2 <= max_size_t}
-> b:lbytes (size 2 *! n1 *! n2)
-> res:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h b /\ live h res /\ disjoint b res)
(ensures fun h0 _ h1 -> modifies1 res h0 h1 /\
as_matrix h1 res == M.matrix_from_lbytes (v n1) (v n2) (as_seq h0 b)) | val matrix_from_lbytes:
#n1:size_t
-> #n2:size_t{2 * v n1 <= max_size_t /\ 2 * v n1 * v n2 <= max_size_t}
-> b:lbytes (size 2 *! n1 *! n2)
-> res:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h b /\ live h res /\ disjoint b res)
(ensures fun h0 _ h1 -> modifies1 res h0 h1 /\
as_matrix h1 res == M.matrix_from_lbytes (v n1) (v n2) (as_seq h0 b)) | let matrix_from_lbytes #n1 #n2 b res =
let h0 = ST.get () in
assert (v n1 * v n2 <= max_size_t);
fill h0 (n1 *! n2) res
(fun h -> M.matrix_from_lbytes_f (v n1) (v n2) (as_seq h0 b))
(fun i ->
assert (2 * v i + 2 <= 2 * v n1 * v n2);
uint_from_bytes_le #U16 (sub b (2ul *! i) 2ul));
let h1 = ST.get () in
M.extensionality (as_matrix h1 res) (M.matrix_from_lbytes (v n1) (v n2) (as_seq h0 b)) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 88,
"end_line": 679,
"start_col": 0,
"start_line": 670
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0
let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b
inline_for_extraction noextract private
val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[i,k] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
private
val onemore: p:(nat -> Type0) -> q:(i:nat{p i} -> Type0) -> b:nat{p b} -> Lemma
(requires (forall (i:nat{p i /\ i < b}). q i) /\ q b)
(ensures forall (i:nat{p i /\ i <= b}). q i)
let onemore p q b = ()
val onemore1:
#n1:size_nat
-> #n3:size_nat{n1 * n3 <= max_size_t}
-> c:M.matrix n1 n3
-> f:(i:nat{i < n1} -> k:nat{k < n3} -> GTot uint16)
-> i:size_nat{i < n1} -> Lemma
(requires ((forall (i1:nat{i1 < i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k) /\ (forall (k:nat{k < n3}). M.mget c i k == f i k)))
(ensures (forall (i1:nat{i1 <= i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k))
let onemore1 #n1 #n3 c f i = ()
val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1 h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall (k:nat{k < v n3}). get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul (as_matrix h0 a) (as_matrix h0 b))
(* Special case of matrix multiplication *)
(* when we have a different way of accessing to entries of the matrix S *)
inline_for_extraction noextract
val mget_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies0 h0 h1 /\
x == M.mget_s (as_matrix h0 a) (v i) (v j))
let mget_s #n1 #n2 a i j =
M.index_lt (v n2) (v n1) (v j) (v i);
a.(j *! n1 +! i)
unfold
let get_s #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget_s (as_matrix h m) i j
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner_s #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get_s h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = mget a i j in
let bjk = mget_s b j k in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)) (v n2);
assert (res == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
inline_for_extraction noextract private
val mul_inner1_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1_s #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)));
mset c i k (mul_inner_s a b i k);
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
val matrix_mul_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul_s (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul_s #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get_s h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get_s h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1_s h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall k. get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul_s (as_matrix h0 a) (as_matrix h0 b))
(* the end of the special matrix multiplication *)
val matrix_eq:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.matrix_eq #(v n1) #(v n2) (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_eq #n1 #n2 a b =
push_frame();
let res = create 1ul (ones U16 SEC) in
let r = buf_eq_mask a b (n1 *! n2) res in
pop_frame ();
r
val matrix_to_lbytes:
#n1:size_t
-> #n2:size_t{2 * v n1 <= max_size_t /\ 2 * v n1 * v n2 <= max_size_t}
-> m:matrix_t n1 n2
-> res:lbytes (2ul *! n1 *! n2)
-> Stack unit
(requires fun h -> live h m /\ live h res /\ disjoint m res)
(ensures fun h0 r h1 -> modifies1 res h0 h1 /\
as_seq h1 res == M.matrix_to_lbytes #(v n1) #(v n2) (as_matrix h0 m))
[@"c_inline"]
let matrix_to_lbytes #n1 #n2 m res =
let h0 = ST.get () in
fill_blocks_simple h0 2ul (n1 *! n2) res
(fun h -> M.matrix_to_lbytes_f #(v n1) #(v n2) (as_matrix h0 m))
(fun i -> uint_to_bytes_le (sub res (2ul *! i) 2ul) m.(i))
val matrix_from_lbytes:
#n1:size_t
-> #n2:size_t{2 * v n1 <= max_size_t /\ 2 * v n1 * v n2 <= max_size_t}
-> b:lbytes (size 2 *! n1 *! n2)
-> res:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h b /\ live h res /\ disjoint b res)
(ensures fun h0 _ h1 -> modifies1 res h0 h1 /\
as_matrix h1 res == M.matrix_from_lbytes (v n1) (v n2) (as_seq h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: Hacl.Impl.Matrix.lbytes (Lib.IntTypes.size 2 *! n1 *! n2) -> res: Hacl.Impl.Matrix.matrix_t n1 n2
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Hacl.Impl.Matrix.lbytes",
"Lib.IntTypes.op_Star_Bang",
"Lib.IntTypes.size",
"Hacl.Impl.Matrix.matrix_t",
"Spec.Matrix.extensionality",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.matrix_from_lbytes",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Lib.IntTypes.uint8",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Lib.Buffer.fill",
"Hacl.Impl.Matrix.elem",
"Spec.Matrix.matrix_from_lbytes_f",
"Lib.IntTypes.size_nat",
"Prims.op_LessThan",
"Lib.ByteBuffer.uint_from_bytes_le",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Lib.IntTypes.uint_t",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.mk_int",
"Lib.Buffer.sub",
"FStar.UInt32.__uint_to_t",
"Prims._assert",
"Prims.op_Addition"
] | [] | false | true | false | false | false | let matrix_from_lbytes #n1 #n2 b res =
| let h0 = ST.get () in
assert (v n1 * v n2 <= max_size_t);
fill h0
(n1 *! n2)
res
(fun h -> M.matrix_from_lbytes_f (v n1) (v n2) (as_seq h0 b))
(fun i ->
assert (2 * v i + 2 <= (2 * v n1) * v n2);
uint_from_bytes_le #U16 (sub b (2ul *! i) 2ul));
let h1 = ST.get () in
M.extensionality (as_matrix h1 res) (M.matrix_from_lbytes (v n1) (v n2) (as_seq h0 b)) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.mul_inner | val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k)) | val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k)) | let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 5,
"end_line": 374,
"start_col": 0,
"start_line": 352
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": 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": 1,
"initial_ifuel": 0,
"max_fuel": 1,
"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 |
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n2 n3 ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
k: Lib.IntTypes.size_t{Lib.IntTypes.v k < Lib.IntTypes.v n3}
-> FStar.HyperStack.ST.Stack Lib.IntTypes.uint16 | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Prims.l_and",
"Hacl.Impl.Matrix.matrix_t",
"Prims.op_LessThan",
"Lib.IntTypes.uint16",
"Prims.unit",
"FStar.HyperStack.ST.pop_frame",
"Prims._assert",
"Prims.eq2",
"Spec.Matrix.mul_inner",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.sum_extensionality",
"Lib.IntTypes.size_nat",
"Lib.IntTypes.op_Star_Dot",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Hacl.Impl.Matrix.get",
"Lib.IntTypes.int_t",
"Lib.Buffer.op_Array_Access",
"Lib.Buffer.MUT",
"Lib.IntTypes.size",
"Lib.Loops.for",
"FStar.Monotonic.HyperStack.mem",
"Prims.nat",
"Lib.Buffer.live",
"Lib.Buffer.modifies1",
"Lib.Buffer.bget",
"Spec.Matrix.sum_",
"Lib.Buffer.op_Array_Assignment",
"Lib.IntTypes.op_Plus_Dot",
"Hacl.Impl.Matrix.op_String_Access",
"FStar.Pervasives.Native.Mktuple2",
"Hacl.Impl.Matrix.elem",
"FStar.HyperStack.ST.get",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.mk_int",
"Lib.Buffer.create",
"Lib.IntTypes.u16",
"Lib.Buffer.lbuffer",
"Prims.op_Subtraction",
"Prims.pow2",
"FStar.HyperStack.ST.push_frame"
] | [] | false | true | false | false | false | let mul_inner #n1 #n2 #n3 a b i k =
| push_frame ();
let h0 = ST.get () in
[@@ inline_let ]let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get () in
Lib.Loops.for (size 0)
n2
(fun h2 j ->
live h1 res /\ live h2 res /\ modifies1 res h1 h2 /\ bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[ i, j ] in
let bjk = b.[ j, k ] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame ();
res | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.matrix_to_lbytes | val matrix_to_lbytes:
#n1:size_t
-> #n2:size_t{2 * v n1 <= max_size_t /\ 2 * v n1 * v n2 <= max_size_t}
-> m:matrix_t n1 n2
-> res:lbytes (2ul *! n1 *! n2)
-> Stack unit
(requires fun h -> live h m /\ live h res /\ disjoint m res)
(ensures fun h0 r h1 -> modifies1 res h0 h1 /\
as_seq h1 res == M.matrix_to_lbytes #(v n1) #(v n2) (as_matrix h0 m)) | val matrix_to_lbytes:
#n1:size_t
-> #n2:size_t{2 * v n1 <= max_size_t /\ 2 * v n1 * v n2 <= max_size_t}
-> m:matrix_t n1 n2
-> res:lbytes (2ul *! n1 *! n2)
-> Stack unit
(requires fun h -> live h m /\ live h res /\ disjoint m res)
(ensures fun h0 r h1 -> modifies1 res h0 h1 /\
as_seq h1 res == M.matrix_to_lbytes #(v n1) #(v n2) (as_matrix h0 m)) | let matrix_to_lbytes #n1 #n2 m res =
let h0 = ST.get () in
fill_blocks_simple h0 2ul (n1 *! n2) res
(fun h -> M.matrix_to_lbytes_f #(v n1) #(v n2) (as_matrix h0 m))
(fun i -> uint_to_bytes_le (sub res (2ul *! i) 2ul) m.(i)) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 60,
"end_line": 656,
"start_col": 0,
"start_line": 652
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0
let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b
inline_for_extraction noextract private
val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[i,k] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
private
val onemore: p:(nat -> Type0) -> q:(i:nat{p i} -> Type0) -> b:nat{p b} -> Lemma
(requires (forall (i:nat{p i /\ i < b}). q i) /\ q b)
(ensures forall (i:nat{p i /\ i <= b}). q i)
let onemore p q b = ()
val onemore1:
#n1:size_nat
-> #n3:size_nat{n1 * n3 <= max_size_t}
-> c:M.matrix n1 n3
-> f:(i:nat{i < n1} -> k:nat{k < n3} -> GTot uint16)
-> i:size_nat{i < n1} -> Lemma
(requires ((forall (i1:nat{i1 < i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k) /\ (forall (k:nat{k < n3}). M.mget c i k == f i k)))
(ensures (forall (i1:nat{i1 <= i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k))
let onemore1 #n1 #n3 c f i = ()
val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1 h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall (k:nat{k < v n3}). get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul (as_matrix h0 a) (as_matrix h0 b))
(* Special case of matrix multiplication *)
(* when we have a different way of accessing to entries of the matrix S *)
inline_for_extraction noextract
val mget_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies0 h0 h1 /\
x == M.mget_s (as_matrix h0 a) (v i) (v j))
let mget_s #n1 #n2 a i j =
M.index_lt (v n2) (v n1) (v j) (v i);
a.(j *! n1 +! i)
unfold
let get_s #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget_s (as_matrix h m) i j
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner_s #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get_s h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = mget a i j in
let bjk = mget_s b j k in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)) (v n2);
assert (res == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
inline_for_extraction noextract private
val mul_inner1_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1_s #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)));
mset c i k (mul_inner_s a b i k);
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
val matrix_mul_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul_s (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul_s #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get_s h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get_s h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1_s h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall k. get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul_s (as_matrix h0 a) (as_matrix h0 b))
(* the end of the special matrix multiplication *)
val matrix_eq:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.matrix_eq #(v n1) #(v n2) (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_eq #n1 #n2 a b =
push_frame();
let res = create 1ul (ones U16 SEC) in
let r = buf_eq_mask a b (n1 *! n2) res in
pop_frame ();
r
val matrix_to_lbytes:
#n1:size_t
-> #n2:size_t{2 * v n1 <= max_size_t /\ 2 * v n1 * v n2 <= max_size_t}
-> m:matrix_t n1 n2
-> res:lbytes (2ul *! n1 *! n2)
-> Stack unit
(requires fun h -> live h m /\ live h res /\ disjoint m res)
(ensures fun h0 r h1 -> modifies1 res h0 h1 /\
as_seq h1 res == M.matrix_to_lbytes #(v n1) #(v n2) (as_matrix h0 m)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | m: Hacl.Impl.Matrix.matrix_t n1 n2 -> res: Hacl.Impl.Matrix.lbytes (2ul *! n1 *! n2)
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Hacl.Impl.Matrix.matrix_t",
"Hacl.Impl.Matrix.lbytes",
"Lib.IntTypes.op_Star_Bang",
"FStar.UInt32.__uint_to_t",
"Lib.Buffer.fill_blocks_simple",
"Lib.IntTypes.uint8",
"FStar.Monotonic.HyperStack.mem",
"Spec.Matrix.matrix_to_lbytes_f",
"Hacl.Impl.Matrix.as_matrix",
"Lib.IntTypes.size_nat",
"Prims.op_LessThan",
"Lib.Sequence.lseq",
"Lib.ByteBuffer.uint_to_bytes_le",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Prims.unit",
"Lib.IntTypes.int_t",
"Lib.Buffer.op_Array_Access",
"Lib.Buffer.MUT",
"Hacl.Impl.Matrix.elem",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.mk_int",
"Lib.Buffer.sub",
"FStar.HyperStack.ST.get"
] | [] | false | true | false | false | false | let matrix_to_lbytes #n1 #n2 m res =
| let h0 = ST.get () in
fill_blocks_simple h0
2ul
(n1 *! n2)
res
(fun h -> M.matrix_to_lbytes_f #(v n1) #(v n2) (as_matrix h0 m))
(fun i -> uint_to_bytes_le (sub res (2ul *! i) 2ul) m.(i)) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.matrix_mul_s | val matrix_mul_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul_s (as_matrix h0 a) (as_matrix h0 b)) | val matrix_mul_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul_s (as_matrix h0 a) (as_matrix h0 b)) | let matrix_mul_s #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get_s h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get_s h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1_s h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall k. get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul_s (as_matrix h0 a) (as_matrix h0 b)) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 79,
"end_line": 618,
"start_col": 0,
"start_line": 595
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0
let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b
inline_for_extraction noextract private
val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[i,k] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
private
val onemore: p:(nat -> Type0) -> q:(i:nat{p i} -> Type0) -> b:nat{p b} -> Lemma
(requires (forall (i:nat{p i /\ i < b}). q i) /\ q b)
(ensures forall (i:nat{p i /\ i <= b}). q i)
let onemore p q b = ()
val onemore1:
#n1:size_nat
-> #n3:size_nat{n1 * n3 <= max_size_t}
-> c:M.matrix n1 n3
-> f:(i:nat{i < n1} -> k:nat{k < n3} -> GTot uint16)
-> i:size_nat{i < n1} -> Lemma
(requires ((forall (i1:nat{i1 < i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k) /\ (forall (k:nat{k < n3}). M.mget c i k == f i k)))
(ensures (forall (i1:nat{i1 <= i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k))
let onemore1 #n1 #n3 c f i = ()
val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1 h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall (k:nat{k < v n3}). get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul (as_matrix h0 a) (as_matrix h0 b))
(* Special case of matrix multiplication *)
(* when we have a different way of accessing to entries of the matrix S *)
inline_for_extraction noextract
val mget_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies0 h0 h1 /\
x == M.mget_s (as_matrix h0 a) (v i) (v j))
let mget_s #n1 #n2 a i j =
M.index_lt (v n2) (v n1) (v j) (v i);
a.(j *! n1 +! i)
unfold
let get_s #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget_s (as_matrix h m) i j
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner_s #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get_s h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = mget a i j in
let bjk = mget_s b j k in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)) (v n2);
assert (res == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
inline_for_extraction noextract private
val mul_inner1_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1_s #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)));
mset c i k (mul_inner_s a b i k);
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
val matrix_mul_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul_s (as_matrix h0 a) (as_matrix h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n2 n3 ->
c: Hacl.Impl.Matrix.matrix_t n1 n3
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Prims.l_and",
"Hacl.Impl.Matrix.matrix_t",
"Spec.Matrix.extensionality",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.mul_s",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Lib.Loops.for",
"Lib.IntTypes.size",
"Prims.nat",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Hacl.Impl.Matrix.elem",
"Lib.Buffer.modifies1",
"Prims.l_Forall",
"Prims.op_LessThan",
"Prims.eq2",
"Lib.IntTypes.uint16",
"Hacl.Impl.Matrix.get",
"Spec.Matrix.elem",
"Prims._assert",
"Hacl.Impl.Matrix.onemore",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.logical",
"Hacl.Impl.Matrix.mul_inner_inv",
"Hacl.Impl.Matrix.mul_inner1_s",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Spec.Matrix.sum",
"Lib.IntTypes.mul_mod",
"Spec.Matrix.mget",
"Lib.Buffer.as_seq",
"Lib.IntTypes.mul",
"Spec.Matrix.mget_s",
"Lib.IntTypes.size_nat",
"Lib.IntTypes.op_Star_Dot",
"Hacl.Impl.Matrix.get_s"
] | [] | false | true | false | false | false | let matrix_mul_s #n1 #n2 #n3 a b c =
| let h0 = ST.get () in
let f (i: nat{i < v n1}) (k: nat{k < v n3})
: GTot (res: uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get_s h0 b l k)}) =
M.sum #(v n2) (fun l -> get h0 a i l *. get_s h0 b l k)
in
Lib.Loops.for (size 0)
n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\ modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1: nat{i1 < i}) (k: nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1: nat{i <= i1 /\ i1 < v n1}) (k: nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get () in
Lib.Loops.for (size 0)
n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1_s h0 h1 a b c i k (f (v i)));
let h1 = ST.get () in
let q i1 = forall k. get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1: nat{i1 < v n1 /\ i1 <= v i}) (k: nat{k < v n3}). get h1 c i1 k == f i1 k));
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.mul_s (as_matrix h0 a) (as_matrix h0 b)) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.matrix_mul | val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b)) | val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b)) | let matrix_mul #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1 h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall (k:nat{k < v n3}). get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul (as_matrix h0 a) (as_matrix h0 b)) | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 77,
"end_line": 489,
"start_col": 0,
"start_line": 466
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0
let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b
inline_for_extraction noextract private
val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[i,k] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
private
val onemore: p:(nat -> Type0) -> q:(i:nat{p i} -> Type0) -> b:nat{p b} -> Lemma
(requires (forall (i:nat{p i /\ i < b}). q i) /\ q b)
(ensures forall (i:nat{p i /\ i <= b}). q i)
let onemore p q b = ()
val onemore1:
#n1:size_nat
-> #n3:size_nat{n1 * n3 <= max_size_t}
-> c:M.matrix n1 n3
-> f:(i:nat{i < n1} -> k:nat{k < n3} -> GTot uint16)
-> i:size_nat{i < n1} -> Lemma
(requires ((forall (i1:nat{i1 < i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k) /\ (forall (k:nat{k < n3}). M.mget c i k == f i k)))
(ensures (forall (i1:nat{i1 <= i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k))
let onemore1 #n1 #n3 c f i = ()
val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n2 n3 ->
c: Hacl.Impl.Matrix.matrix_t n1 n3
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Prims.l_and",
"Hacl.Impl.Matrix.matrix_t",
"Spec.Matrix.extensionality",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.mul",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Lib.Loops.for",
"Lib.IntTypes.size",
"Prims.nat",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Hacl.Impl.Matrix.elem",
"Lib.Buffer.modifies1",
"Prims.l_Forall",
"Prims.op_LessThan",
"Prims.eq2",
"Lib.IntTypes.uint16",
"Hacl.Impl.Matrix.get",
"Spec.Matrix.elem",
"Prims._assert",
"Hacl.Impl.Matrix.onemore",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.logical",
"Hacl.Impl.Matrix.mul_inner_inv",
"Hacl.Impl.Matrix.mul_inner1",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Spec.Matrix.sum",
"Lib.IntTypes.mul_mod",
"Spec.Matrix.mget",
"Lib.Buffer.as_seq",
"Lib.IntTypes.mul",
"Lib.IntTypes.size_nat",
"Lib.IntTypes.op_Star_Dot"
] | [] | false | true | false | false | false | let matrix_mul #n1 #n2 #n3 a b c =
| let h0 = ST.get () in
let f (i: nat{i < v n1}) (k: nat{k < v n3})
: GTot (res: uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)}) =
M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)
in
Lib.Loops.for (size 0)
n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\ modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1: nat{i1 < i}) (k: nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1: nat{i <= i1 /\ i1 < v n1}) (k: nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get () in
Lib.Loops.for (size 0)
n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1 h0 h1 a b c i k (f (v i)));
let h1 = ST.get () in
let q i1 = forall (k: nat{k < v n3}). get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1: nat{i1 < v n1 /\ i1 <= v i}) (k: nat{k < v n3}). get h1 c i1 k == f i1 k));
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.mul (as_matrix h0 a) (as_matrix h0 b)) | false |
Hacl.Impl.Matrix.fst | Hacl.Impl.Matrix.mul_inner_s | val mul_inner_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k)) | val mul_inner_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k)) | let mul_inner_s #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get_s h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = mget a i j in
let bjk = mget_s b j k in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)) (v n2);
assert (res == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res | {
"file_name": "code/frodo/Hacl.Impl.Matrix.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 5,
"end_line": 550,
"start_col": 0,
"start_line": 529
} | module Hacl.Impl.Matrix
open FStar.HyperStack.ST
open FStar.Mul
open LowStar.BufferOps
open LowStar.Buffer
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
module HS = FStar.HyperStack
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BSeq = Lib.ByteSequence
module Loops = Lib.LoopCombinators
module M = Spec.Matrix
module Lemmas = Spec.Frodo.Lemmas
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let elem = uint16
unfold
let lbytes len = lbuffer uint8 len
unfold
let matrix_t (n1:size_t) (n2:size_t{v n1 * v n2 <= max_size_t}) =
lbuffer elem (n1 *! n2)
unfold
let as_matrix #n1 #n2 h (m:matrix_t n1 n2) : GTot (M.matrix (v n1) (v n2)) =
as_seq h m
inline_for_extraction noextract
val matrix_create:
n1:size_t
-> n2:size_t{0 < v n1 * v n2 /\ v n1 * v n2 <= max_size_t}
-> StackInline (matrix_t n1 n2)
(requires fun h0 -> True)
(ensures fun h0 a h1 ->
stack_allocated a h0 h1 (as_matrix h1 a) /\
as_matrix h1 a == M.create (v n1) (v n2))
let matrix_create n1 n2 =
[@inline_let]
let len = size (normalize_term (v n1 * v n2)) in
create len (u16 0)
inline_for_extraction noextract
val mget:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies loc_none h0 h1 /\
x == M.mget (as_matrix h0 a) (v i) (v j))
let mget #n1 #n2 a i j =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j)
inline_for_extraction noextract
val mset:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> x:elem
-> Stack unit
(requires fun h0 -> live h0 a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mset (as_matrix h0 a) (v i) (v j) x)
let mset #n1 #n2 a i j x =
M.index_lt (v n1) (v n2) (v i) (v j);
a.(i *! n2 +! j) <- x
noextract unfold
val op_String_Access (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2})
: Stack elem
(requires fun h0 -> live h0 m)
(ensures fun h0 x h1 -> let i, j = ij in modifies loc_none h0 h1 /\ x == M.mget (as_matrix h0 m) (v i) (v j))
let op_String_Access #n1 #n2 m (i,j) = mget m i j
noextract unfold
val op_String_Assignment (#n1:size_t) (#n2:size_t{v n1 * v n2 <= max_size_t}) (m:matrix_t n1 n2) (ij:(size_t & size_t){let i, j = ij in v i < v n1 /\ v j < v n2}) (x:elem)
: Stack unit
(requires fun h0 -> live h0 m)
(ensures fun h0 _ h1 -> let i, j = ij in modifies1 m h0 h1 /\ live h1 m /\ as_matrix h1 m == M.mset (as_matrix h0 m) (v i) (v j) x)
let op_String_Assignment #n1 #n2 m (i,j) x = mset m i j x
unfold
let get #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget (as_matrix h m) i j
private unfold
val map_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map_inner_inv #n1 #n2 h0 h1 h2 f a c i j =
live h2 a /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map_inner_inv h0 h1 h2 f a c i (v j))
(ensures fun _ _ h2 -> map_inner_inv h0 h1 h2 f a c i (v j + 1))
let map_inner #n1 #n2 h0 h1 f a c i j =
c.[i,j] <- f a.[i,j]
inline_for_extraction
val map:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16)
-> a:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 c /\ a == c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map #(v n1) #(v n2) f (as_matrix h0 a))
let map #n1 #n2 f a c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map_inner_inv h0 h1 h2 f a c i j)
(fun j -> map_inner h0 h1 f a c i j)
);
let h2 = ST.get () in
M.extensionality (as_matrix h2 c) (M.map f (as_matrix h0 a))
inline_for_extraction noextract
val mod_pow2_felem:
logq:size_t{0 < v logq /\ v logq < 16}
-> a:uint16
-> Pure uint16
(requires True)
(ensures fun r -> r == M.mod_pow2_felem (v logq) a)
let mod_pow2_felem logq a =
a &. ((u16 1 <<. logq) -. u16 1)
val mod_pow2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> logq:size_t{0 < v logq /\ v logq <= 16}
-> a:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a)
(ensures fun h0 _ h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.mod_pow2 (v logq) (as_matrix h0 a))
[@"c_inline"]
let mod_pow2 #n1 #n2 logq a =
if logq <. 16ul then begin
let h0 = ST.get () in
map (mod_pow2_felem logq) a a;
M.extensionality
(M.map #(v n1) #(v n2) (mod_pow2_felem logq) (as_matrix h0 a))
(M.map #(v n1) #(v n2) (M.mod_pow2_felem (v logq)) (as_matrix h0 a)) end
private unfold
val map2_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_nat
-> Type0
let map2_inner_inv #n1 #n2 h0 h1 h2 f a b c i j =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
j <= v n2 /\
(forall (i0:nat{i0 < v i}) (j:nat{j < v n2}). get h2 c i0 j == get h1 c i0 j) /\
(forall (j0:nat{j0 < j}). get h2 c (v i) j0 == f (get h0 a (v i) j0) (get h2 b (v i) j0)) /\
(forall (j0:nat{j <= j0 /\ j0 < v n2}). get h2 c (v i) j0 == get h0 c (v i) j0) /\
(forall (i0:nat{v i < i0 /\ i0 < v n1}) (j:nat{j < v n2}). get h2 c i0 j == get h0 c i0 j)
inline_for_extraction noextract private
val map2_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> f:(elem -> elem -> elem)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2{a == c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack unit
(requires fun h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j))
(ensures fun _ _ h2 -> map2_inner_inv h0 h1 h2 f a b c i (v j + 1))
let map2_inner #n1 #n2 h0 h1 f a b c i j =
c.[i,j] <- f a.[i,j] b.[i,j]
/// In-place [map2], a == map2 f a b
///
/// A non in-place variant can be obtained by weakening the pre-condition to disjoint a c,
/// or the two variants can be merged by requiring (a == c \/ disjoint a c) instead of a == c
/// See commit 91916b8372fa3522061eff5a42d0ebd1d19a8a49
inline_for_extraction
val map2:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> f:(uint16 -> uint16 -> uint16)
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> c:matrix_t n1 n2
-> Stack unit
(requires fun h0 ->
live h0 a /\ live h0 b /\ live h0 c /\
a == c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.map2 #(v n1) #(v n2) f (as_matrix h0 a) (as_matrix h0 b))
let map2 #n1 #n2 f a b c =
let h0 = ST.get () in
Lib.Loops.for (size 0) n1
(fun h1 i -> live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i0:nat{i0 < i}) (j:nat{j < v n2}).
get h1 c i0 j == f (get h0 a i0 j) (get h0 b i0 j)) /\
(forall (i0:nat{i <= i0 /\ i0 < v n1}) (j:nat{j < v n2}).
get h1 c i0 j == get h0 c i0 j) )
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> map2_inner_inv h0 h1 h2 f a b c i j)
(fun j -> map2_inner h0 h1 f a b c i j)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.map2 f (as_matrix h0 a) (as_matrix h0 b))
val matrix_add:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 a h0 h1 /\
as_matrix h1 a == M.add (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_add #n1 #n2 a b =
map2 add_mod a b a
val matrix_sub:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n1 n2
-> Stack unit
(requires fun h -> live h a /\ live h b /\ disjoint a b)
(ensures fun h0 r h1 -> modifies1 b h0 h1 /\
as_matrix h1 b == M.sub (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_sub #n1 #n2 a b =
(* Use the in-place variant above by flipping the arguments of [sub_mod] *)
(* Requires appplying extensionality *)
let h0 = ST.get() in
[@ inline_let ]
let sub_mod_flipped x y = sub_mod y x in
map2 sub_mod_flipped b a b;
let h1 = ST.get() in
M.extensionality (as_matrix h1 b) (M.sub (as_matrix h0 a) (as_matrix h0 b))
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies loc_none h0 h1 /\
r == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k))
let mul_inner #n1 #n2 #n3 a b i k =
push_frame();
let h0 = ST.get() in
[@ inline_let ]
let f l = get h0 a (v i) l *. get h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get() in
Lib.Loops.for (size 0) n2
(fun h2 j -> live h1 res /\ live h2 res /\
modifies1 res h1 h2 /\
bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = a.[i,j] in
let bjk = b.[j,k] in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk
);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get h0 b l (v k)) (v n2);
assert (res == M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame();
res
#pop-options
private unfold
val mul_inner_inv:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> h2:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> f:(k:nat{k < v n3} -> GTot uint16)
-> i:size_t{v i < v n1}
-> k:size_nat
-> Type0
let mul_inner_inv #n1 #n2 #n3 h0 h1 h2 a b c f i k =
live h2 a /\ live h2 b /\ live h2 c /\
modifies1 c h1 h2 /\
k <= v n3 /\
(forall (i1:nat{i1 < v i}) (k:nat{k < v n3}). get h2 c i1 k == get h1 c i1 k) /\
(forall (k1:nat{k1 < k}). get h2 c (v i) k1 == f k1) /\
(forall (k1:nat{k <= k1 /\ k1 < v n3}). get h2 c (v i) k1 == get h0 c (v i) k1) /\
(forall (i1:nat{v i < i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h2 c i1 k == get h0 c i1 k) /\
as_matrix h0 a == as_matrix h2 a /\
as_matrix h0 b == as_matrix h2 b
inline_for_extraction noextract private
val mul_inner1:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> h0:HS.mem
-> h1:HS.mem
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3{disjoint a c /\ disjoint b c}
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> f:(k:nat{k < v n3}
-> GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l k)}))
-> Stack unit
(requires fun h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k))
(ensures fun _ _ h2 -> mul_inner_inv h0 h1 h2 a b c f i (v k + 1))
let mul_inner1 #n1 #n2 #n3 h0 h1 a b c i k f =
assert (M.mul_inner (as_matrix h0 a) (as_matrix h0 b) (v i) (v k) ==
M.sum #(v n2) (fun l -> get h0 a (v i) l *. get h0 b l (v k)));
c.[i,k] <- mul_inner a b i k;
let h2 = ST.get () in
assert (get h2 c (v i) (v k) == f (v k))
private
val onemore: p:(nat -> Type0) -> q:(i:nat{p i} -> Type0) -> b:nat{p b} -> Lemma
(requires (forall (i:nat{p i /\ i < b}). q i) /\ q b)
(ensures forall (i:nat{p i /\ i <= b}). q i)
let onemore p q b = ()
val onemore1:
#n1:size_nat
-> #n3:size_nat{n1 * n3 <= max_size_t}
-> c:M.matrix n1 n3
-> f:(i:nat{i < n1} -> k:nat{k < n3} -> GTot uint16)
-> i:size_nat{i < n1} -> Lemma
(requires ((forall (i1:nat{i1 < i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k) /\ (forall (k:nat{k < n3}). M.mget c i k == f i k)))
(ensures (forall (i1:nat{i1 <= i}) (k:nat{k < n3}). M.mget c i1 k == f i1 k))
let onemore1 #n1 #n3 c f i = ()
val matrix_mul:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> c:matrix_t n1 n3
-> Stack unit
(requires fun h ->
live h a /\ live h b /\ live h c /\
disjoint a c /\ disjoint b c)
(ensures fun h0 _ h1 -> modifies1 c h0 h1 /\
as_matrix h1 c == M.mul (as_matrix h0 a) (as_matrix h0 b))
[@"c_inline"]
let matrix_mul #n1 #n2 #n3 a b c =
let h0 = ST.get () in
let f (i:nat{i < v n1}) (k:nat{k < v n3}) :
GTot (res:uint16{res == M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)})
= M.sum #(v n2) (fun l -> get h0 a i l *. get h0 b l k)
in
Lib.Loops.for (size 0) n1
(fun h1 i ->
live h1 a /\ live h1 b /\ live h1 c /\
modifies1 c h0 h1 /\ i <= v n1 /\
(forall (i1:nat{i1 < i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k) /\
(forall (i1:nat{i <= i1 /\ i1 < v n1}) (k:nat{k < v n3}). get h1 c i1 k == get h0 c i1 k))
(fun i ->
let h1 = ST.get() in
Lib.Loops.for (size 0) n3
(fun h2 k -> mul_inner_inv h0 h1 h2 a b c (f (v i)) i k)
(fun k -> mul_inner1 h0 h1 a b c i k (f (v i)));
let h1 = ST.get() in
let q i1 = forall (k:nat{k < v n3}). get h1 c i1 k == f i1 k in
onemore (fun i1 -> i1 < v n1) q (v i);
assert (forall (i1:nat{i1 < v n1 /\ i1 <= v i}) (k:nat{k < v n3}). get h1 c i1 k == f i1 k)
);
let h2 = ST.get() in
M.extensionality (as_matrix h2 c) (M.mul (as_matrix h0 a) (as_matrix h0 b))
(* Special case of matrix multiplication *)
(* when we have a different way of accessing to entries of the matrix S *)
inline_for_extraction noextract
val mget_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> a:matrix_t n1 n2
-> i:size_t{v i < v n1}
-> j:size_t{v j < v n2}
-> Stack elem
(requires fun h0 -> live h0 a)
(ensures fun h0 x h1 -> modifies0 h0 h1 /\
x == M.mget_s (as_matrix h0 a) (v i) (v j))
let mget_s #n1 #n2 a i j =
M.index_lt (v n2) (v n1) (v j) (v i);
a.(j *! n1 +! i)
unfold
let get_s #n1 #n2 h (m:matrix_t n1 n2) i j = M.mget_s (as_matrix h m) i j
#push-options "--fuel 1"
inline_for_extraction noextract private
val mul_inner_s:
#n1:size_t
-> #n2:size_t{v n1 * v n2 <= max_size_t}
-> #n3:size_t{v n2 * v n3 <= max_size_t /\ v n1 * v n3 <= max_size_t}
-> a:matrix_t n1 n2
-> b:matrix_t n2 n3
-> i:size_t{v i < v n1}
-> k:size_t{v k < v n3}
-> Stack uint16
(requires fun h -> live h a /\ live h b)
(ensures fun h0 r h1 -> modifies0 h0 h1 /\
r == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k)) | {
"checked_file": "/",
"dependencies": [
"Spec.Matrix.fst.checked",
"Spec.Frodo.Lemmas.fst.checked",
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Matrix.fst"
} | [
{
"abbrev": true,
"full_module": "Spec.Frodo.Lemmas",
"short_module": "Lemmas"
},
{
"abbrev": true,
"full_module": "Spec.Matrix",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.ByteSequence",
"short_module": "BSeq"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"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": "LowStar.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": 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": 1,
"initial_ifuel": 0,
"max_fuel": 1,
"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 |
a: Hacl.Impl.Matrix.matrix_t n1 n2 ->
b: Hacl.Impl.Matrix.matrix_t n2 n3 ->
i: Lib.IntTypes.size_t{Lib.IntTypes.v i < Lib.IntTypes.v n1} ->
k: Lib.IntTypes.size_t{Lib.IntTypes.v k < Lib.IntTypes.v n3}
-> FStar.HyperStack.ST.Stack Lib.IntTypes.uint16 | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.max_size_t",
"Prims.l_and",
"Hacl.Impl.Matrix.matrix_t",
"Prims.op_LessThan",
"Lib.IntTypes.uint16",
"Prims.unit",
"FStar.HyperStack.ST.pop_frame",
"Prims._assert",
"Prims.eq2",
"Spec.Matrix.mul_inner_s",
"Hacl.Impl.Matrix.as_matrix",
"Spec.Matrix.sum_extensionality",
"Lib.IntTypes.size_nat",
"Lib.IntTypes.op_Star_Dot",
"Lib.IntTypes.U16",
"Lib.IntTypes.SEC",
"Hacl.Impl.Matrix.get",
"Hacl.Impl.Matrix.get_s",
"Lib.IntTypes.int_t",
"Lib.Buffer.op_Array_Access",
"Lib.Buffer.MUT",
"Lib.IntTypes.size",
"Lib.Loops.for",
"FStar.Monotonic.HyperStack.mem",
"Prims.nat",
"Lib.Buffer.live",
"Lib.Buffer.modifies1",
"Lib.Buffer.bget",
"Spec.Matrix.sum_",
"Lib.Buffer.op_Array_Assignment",
"Lib.IntTypes.op_Plus_Dot",
"Hacl.Impl.Matrix.mget_s",
"Hacl.Impl.Matrix.elem",
"Hacl.Impl.Matrix.mget",
"FStar.HyperStack.ST.get",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.mk_int",
"Lib.Buffer.create",
"Lib.IntTypes.u16",
"Lib.Buffer.lbuffer",
"Prims.op_Subtraction",
"Prims.pow2",
"FStar.HyperStack.ST.push_frame"
] | [] | false | true | false | false | false | let mul_inner_s #n1 #n2 #n3 a b i k =
| push_frame ();
let h0 = ST.get () in
[@@ inline_let ]let f l = get h0 a (v i) l *. get_s h0 b l (v k) in
let res = create #uint16 (size 1) (u16 0) in
let h1 = ST.get () in
Lib.Loops.for (size 0)
n2
(fun h2 j ->
live h1 res /\ live h2 res /\ modifies1 res h1 h2 /\ bget h2 res 0 == M.sum_ #(v n2) f j)
(fun j ->
let aij = mget a i j in
let bjk = mget_s b j k in
let res0 = res.(size 0) in
res.(size 0) <- res0 +. aij *. bjk);
let res = res.(size 0) in
M.sum_extensionality (v n2) f (fun l -> get h0 a (v i) l *. get_s h0 b l (v k)) (v n2);
assert (res == M.mul_inner_s (as_matrix h0 a) (as_matrix h0 b) (v i) (v k));
pop_frame ();
res | false |
Steel.Utils.fst | Steel.Utils.pure_as_ens | val pure_as_ens: #p: prop -> Prims.unit
-> Steel unit (pure p) (fun _ -> pure p) (fun _ -> True) (fun _ _ _ -> p) | val pure_as_ens: #p: prop -> Prims.unit
-> Steel unit (pure p) (fun _ -> pure p) (fun _ -> True) (fun _ _ _ -> p) | let pure_as_ens (#p:prop) ()
: Steel unit (pure p) (fun _ -> pure p) (fun _ -> True) (fun _ _ _ -> p)
= change_slprop_ens (pure p) (pure p) p (Steel.Memory.pure_interp p) | {
"file_name": "lib/steel/Steel.Utils.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 70,
"end_line": 48,
"start_col": 0,
"start_line": 46
} | (*
Copyright 2020 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 Steel.Utils
open Steel.Memory
open Steel.Effect.Atomic
open Steel.Effect
open Steel.FractionalPermission
open Steel.Reference
let pts_to_not_null (#a:Type)
(#opened:inames)
(#p:perm)
(#v:FStar.Ghost.erased a)
(r:ref a)
: SteelGhost unit opened
(pts_to r p v)
(fun _ -> pts_to r p v)
(fun _ -> True)
(fun _ _ _ -> r =!= null)
= extract_info_raw (pts_to r p v) (r =!= null)
(fun m -> pts_to_not_null r p v m)
let change_slprop_ens (p:vprop) (q:vprop) (r:prop) (f:(m:mem -> Lemma (requires interp (hp_of p) m)
(ensures interp (hp_of q) m /\ r)))
: Steel unit p (fun _ -> q) (requires fun _ -> True) (ensures fun _ _ _ -> r)
= rewrite_slprop p (q `star` pure r)
(fun m -> f m;
Steel.Memory.emp_unit (hp_of q);
Steel.Memory.pure_star_interp (hp_of q) r m);
elim_pure r | {
"checked_file": "/",
"dependencies": [
"Steel.Reference.fsti.checked",
"Steel.Memory.fsti.checked",
"Steel.FractionalPermission.fst.checked",
"Steel.Effect.Atomic.fsti.checked",
"Steel.Effect.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Steel.Utils.fst"
} | [
{
"abbrev": false,
"full_module": "Steel.Reference",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.FractionalPermission",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect.Atomic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel",
"short_module": null
},
{
"abbrev": 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 -> Steel.Effect.Steel Prims.unit | Steel.Effect.Steel | [] | [] | [
"Prims.prop",
"Prims.unit",
"Steel.Utils.change_slprop_ens",
"Steel.Effect.Common.pure",
"Steel.Memory.pure_interp",
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.rmem",
"Prims.l_True"
] | [] | false | true | false | false | false | let pure_as_ens (#p: prop) ()
: Steel unit (pure p) (fun _ -> pure p) (fun _ -> True) (fun _ _ _ -> p) =
| change_slprop_ens (pure p) (pure p) p (Steel.Memory.pure_interp p) | false |
Steel.Utils.fst | Steel.Utils.rewrite | val rewrite (#a: _) (#p: (a -> vprop)) (x y: a)
: Steel unit (p x) (fun _ -> p y) (requires fun _ -> x == y) (ensures fun _ _ _ -> True) | val rewrite (#a: _) (#p: (a -> vprop)) (x y: a)
: Steel unit (p x) (fun _ -> p y) (requires fun _ -> x == y) (ensures fun _ _ _ -> True) | let rewrite #a (#p:a -> vprop) (x y:a)
: Steel unit (p x) (fun _ -> p y)
(requires fun _ -> x == y)
(ensures fun _ _ _ -> True)
= rewrite_slprop (p x) (p y) (fun _ -> ()) | {
"file_name": "lib/steel/Steel.Utils.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 44,
"end_line": 54,
"start_col": 0,
"start_line": 50
} | (*
Copyright 2020 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 Steel.Utils
open Steel.Memory
open Steel.Effect.Atomic
open Steel.Effect
open Steel.FractionalPermission
open Steel.Reference
let pts_to_not_null (#a:Type)
(#opened:inames)
(#p:perm)
(#v:FStar.Ghost.erased a)
(r:ref a)
: SteelGhost unit opened
(pts_to r p v)
(fun _ -> pts_to r p v)
(fun _ -> True)
(fun _ _ _ -> r =!= null)
= extract_info_raw (pts_to r p v) (r =!= null)
(fun m -> pts_to_not_null r p v m)
let change_slprop_ens (p:vprop) (q:vprop) (r:prop) (f:(m:mem -> Lemma (requires interp (hp_of p) m)
(ensures interp (hp_of q) m /\ r)))
: Steel unit p (fun _ -> q) (requires fun _ -> True) (ensures fun _ _ _ -> r)
= rewrite_slprop p (q `star` pure r)
(fun m -> f m;
Steel.Memory.emp_unit (hp_of q);
Steel.Memory.pure_star_interp (hp_of q) r m);
elim_pure r
let pure_as_ens (#p:prop) ()
: Steel unit (pure p) (fun _ -> pure p) (fun _ -> True) (fun _ _ _ -> p)
= change_slprop_ens (pure p) (pure p) p (Steel.Memory.pure_interp p) | {
"checked_file": "/",
"dependencies": [
"Steel.Reference.fsti.checked",
"Steel.Memory.fsti.checked",
"Steel.FractionalPermission.fst.checked",
"Steel.Effect.Atomic.fsti.checked",
"Steel.Effect.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Steel.Utils.fst"
} | [
{
"abbrev": false,
"full_module": "Steel.Reference",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.FractionalPermission",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect.Atomic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel",
"short_module": null
},
{
"abbrev": 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 -> y: a -> Steel.Effect.Steel Prims.unit | Steel.Effect.Steel | [] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Atomic.rewrite_slprop",
"FStar.Ghost.hide",
"FStar.Set.set",
"Steel.Memory.iname",
"FStar.Set.empty",
"Steel.Memory.mem",
"Prims.unit",
"Steel.Effect.Common.rmem",
"Prims.eq2",
"Prims.l_True"
] | [] | false | true | false | false | false | let rewrite #a (#p: (a -> vprop)) (x: a) (y: a)
: Steel unit (p x) (fun _ -> p y) (requires fun _ -> x == y) (ensures fun _ _ _ -> True) =
| rewrite_slprop (p x) (p y) (fun _ -> ()) | false |
Steel.Utils.fst | Steel.Utils.pts_to_not_null | val pts_to_not_null (#a: Type) (#opened: inames) (#p: perm) (#v: FStar.Ghost.erased a) (r: ref a)
: SteelGhost unit
opened
(pts_to r p v)
(fun _ -> pts_to r p v)
(fun _ -> True)
(fun _ _ _ -> r =!= null) | val pts_to_not_null (#a: Type) (#opened: inames) (#p: perm) (#v: FStar.Ghost.erased a) (r: ref a)
: SteelGhost unit
opened
(pts_to r p v)
(fun _ -> pts_to r p v)
(fun _ -> True)
(fun _ _ _ -> r =!= null) | let pts_to_not_null (#a:Type)
(#opened:inames)
(#p:perm)
(#v:FStar.Ghost.erased a)
(r:ref a)
: SteelGhost unit opened
(pts_to r p v)
(fun _ -> pts_to r p v)
(fun _ -> True)
(fun _ _ _ -> r =!= null)
= extract_info_raw (pts_to r p v) (r =!= null)
(fun m -> pts_to_not_null r p v m) | {
"file_name": "lib/steel/Steel.Utils.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 40,
"end_line": 35,
"start_col": 0,
"start_line": 24
} | (*
Copyright 2020 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 Steel.Utils
open Steel.Memory
open Steel.Effect.Atomic
open Steel.Effect
open Steel.FractionalPermission
open Steel.Reference | {
"checked_file": "/",
"dependencies": [
"Steel.Reference.fsti.checked",
"Steel.Memory.fsti.checked",
"Steel.FractionalPermission.fst.checked",
"Steel.Effect.Atomic.fsti.checked",
"Steel.Effect.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Steel.Utils.fst"
} | [
{
"abbrev": false,
"full_module": "Steel.Reference",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.FractionalPermission",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect.Atomic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel",
"short_module": null
},
{
"abbrev": 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: Steel.Reference.ref a -> Steel.Effect.Atomic.SteelGhost Prims.unit | Steel.Effect.Atomic.SteelGhost | [] | [] | [
"Steel.Memory.inames",
"Steel.FractionalPermission.perm",
"FStar.Ghost.erased",
"Steel.Reference.ref",
"Steel.Effect.Atomic.extract_info_raw",
"Steel.Reference.pts_to",
"FStar.Ghost.reveal",
"Prims.l_not",
"Prims.eq2",
"Steel.Reference.null",
"Steel.Memory.mem",
"Steel.Reference.pts_to_not_null",
"Prims.unit",
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.rmem",
"Prims.l_True"
] | [] | false | true | false | false | false | let pts_to_not_null (#a: Type) (#opened: inames) (#p: perm) (#v: FStar.Ghost.erased a) (r: ref a)
: SteelGhost unit
opened
(pts_to r p v)
(fun _ -> pts_to r p v)
(fun _ -> True)
(fun _ _ _ -> r =!= null) =
| extract_info_raw (pts_to r p v) (r =!= null) (fun m -> pts_to_not_null r p v m) | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.synth_bv8_recip | val synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t | val synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t | let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 12,
"end_line": 75,
"start_col": 0,
"start_line": 74
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.BitVector.bv_t 8 -> FStar.UInt8.t | Prims.Tot | [
"total"
] | [] | [
"FStar.BitVector.bv_t",
"LowParse.Spec.BitVector.to_uint8",
"FStar.UInt8.t"
] | [] | false | false | false | false | false | let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
| to_uint8 x | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.parse_bv8_kind | val parse_bv8_kind : LowParse.Spec.Base.parser_kind | let parse_bv8_kind = parse_u8_kind | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 34,
"end_line": 87,
"start_col": 0,
"start_line": 87
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x
let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
)
let synth_bv8_inverse : squash (synth_inverse synth_bv8 synth_bv8_recip) =
synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x ->
of_uint8_to_uint8 x
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Int.parse_u8_kind"
] | [] | false | false | false | true | false | let parse_bv8_kind =
| parse_u8_kind | false |
|
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.parse_extra_bv8_kind | val parse_extra_bv8_kind (n: nat) : Tot parser_kind | val parse_extra_bv8_kind (n: nat) : Tot parser_kind | let parse_extra_bv8_kind (n: nat) : Tot parser_kind =
parse_filter_kind parse_u8_kind | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 33,
"end_line": 187,
"start_col": 0,
"start_line": 186
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x
let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
)
let synth_bv8_inverse : squash (synth_inverse synth_bv8 synth_bv8_recip) =
synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x ->
of_uint8_to_uint8 x
)
let parse_bv8_kind = parse_u8_kind
let parse_bv8 : parser parse_bv8_kind (BV.bv_t 8) =
parse_u8 `parse_synth` synth_bv8
let serialize_bv8 : serializer parse_bv8 =
serialize_synth
parse_u8
synth_bv8
serialize_u8
synth_bv8_recip
()
(* parse a 8*n bit vector *)
let synth_byte_bv (n: nat) (x: (BV.bv_t 8 & BV.bv_t (8 * n))) : Tot (BV.bv_t (8 * (1 + n))) =
Seq.append (fst x) (snd x)
let synth_byte_bv_recip (n: nat) (x: BV.bv_t (8 * (1 + n))) : Tot (BV.bv_t 8 & BV.bv_t (8 * n)) =
Seq.slice x 0 8, Seq.slice x 8 (Seq.length x)
let synth_byte_bv_injective (n: nat) : Lemma
(synth_injective (synth_byte_bv n))
[SMTPat (synth_injective (synth_byte_bv n))]
= synth_inverse_intro' (synth_byte_bv_recip n) (synth_byte_bv n) (fun x ->
let (hd, tl) = x in
let (hd', tl') = synth_byte_bv_recip n (synth_byte_bv n x) in
assert (hd `Seq.equal` hd');
assert (tl `Seq.equal` tl')
)
let synth_byte_bv_inverse (n: nat) : Lemma
(synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))
[SMTPat (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))]
= synth_inverse_intro' (synth_byte_bv n) (synth_byte_bv_recip n) (fun x ->
assert (synth_byte_bv n (synth_byte_bv_recip n x) `Seq.equal` x)
)
let rec parse_byte_bv_kind' (n: nat) : Tot parser_kind =
if n = 0
then parse_ret_kind
else parse_bv8_kind `and_then_kind` parse_byte_bv_kind' (n - 1)
let parse_byte_bv_kind (n: nat) : Tot parser_kind =
strong_parser_kind n n (Some ParserKindMetadataTotal)
let rec parse_byte_bv_kind_eq (n: nat) : Lemma
(parse_byte_bv_kind n == parse_byte_bv_kind' n)
= if n = 0
then ()
else parse_byte_bv_kind_eq (n - 1)
let rec parse_byte_bv (n: nat) : Tot (parser (parse_byte_bv_kind n) (BV.bv_t (8 * n))) =
parse_byte_bv_kind_eq n;
if n = 0
then
parse_ret Seq.empty
else
(parse_bv8 `nondep_then` parse_byte_bv (n - 1)) `parse_synth` synth_byte_bv (n - 1)
let rec serialize_byte_bv (n: nat) : Tot (serializer (parse_byte_bv n)) =
parse_byte_bv_kind_eq n;
if n = 0
then
serialize_ret Seq.empty (fun (x: BV.bv_t 0) -> assert (x `Seq.equal` Seq.empty))
else
serialize_synth
(parse_bv8 `nondep_then` parse_byte_bv (n - 1))
(synth_byte_bv (n - 1))
(serialize_bv8 `serialize_nondep_then` serialize_byte_bv (n - 1))
(synth_byte_bv_recip (n - 1))
()
(* parse extra bits (big-endian with the first bits equal to 0) *)
let extra_bytes_prop (n: nat) (x: U8.t) : Tot bool =
U8.v x < pow2 (n % 8)
let synth_extra_bv8 (n: nat) (x: parse_filter_refine (extra_bytes_prop n)) : Tot (BV.bv_t (n % 8)) =
of_uint8 (n % 8) x
let synth_extra_bv8_recip (n: nat) (x: BV.bv_t (n % 8)) : Tot (parse_filter_refine (extra_bytes_prop n)) =
to_uint8 x
let synth_extra_bv8_injective (n: nat) : Lemma (synth_injective (synth_extra_bv8 n))
[SMTPat (synth_injective (synth_extra_bv8 n))]
=
synth_inverse_intro' (synth_extra_bv8_recip n) (synth_extra_bv8 n) (fun x ->
to_uint8_of_uint8 (n % 8) x
)
let synth_extra_bv8_inverse (n: nat) : Lemma (synth_inverse (synth_extra_bv8 n) (synth_extra_bv8_recip n))
[SMTPat (synth_inverse (synth_extra_bv8 n) (synth_extra_bv8_recip n))]
=
synth_inverse_intro' (synth_extra_bv8 n) (synth_extra_bv8_recip n) (fun x ->
of_uint8_to_uint8 x
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | n: Prims.nat -> LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"LowParse.Spec.Combinators.parse_filter_kind",
"LowParse.Spec.Int.parse_u8_kind",
"LowParse.Spec.Base.parser_kind"
] | [] | false | false | false | true | false | let parse_extra_bv8_kind (n: nat) : Tot parser_kind =
| parse_filter_kind parse_u8_kind | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.of_uint8 | val of_uint8 (n: nat{n <= 8}) (x: U8.t{U8.v x < pow2 n}) : Tot (BV.bv_t n) | val of_uint8 (n: nat{n <= 8}) (x: U8.t{U8.v x < pow2 n}) : Tot (BV.bv_t n) | let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy) | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 38,
"end_line": 30,
"start_col": 0,
"start_line": 22
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | n: Prims.nat{n <= 8} -> x: FStar.UInt8.t{FStar.UInt8.v x < Prims.pow2 n} -> FStar.BitVector.bv_t n | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt8.t",
"Prims.op_LessThan",
"FStar.UInt8.v",
"Prims.pow2",
"Prims.op_Equality",
"Prims.int",
"FStar.Seq.Base.empty",
"Prims.bool",
"FStar.Seq.Properties.snoc",
"FStar.UInt8.rem",
"FStar.UInt8.__uint_to_t",
"FStar.BitVector.bv_t",
"Prims.op_Subtraction",
"LowParse.Spec.BitVector.of_uint8",
"FStar.UInt8.div"
] | [
"recursion"
] | false | false | false | false | false | let rec of_uint8 (n: nat{n <= 8}) (x: U8.t{U8.v x < pow2 n}) : Tot (BV.bv_t n) =
| if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy) | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.synth_bv8 | val synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) | val synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) | let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 14,
"end_line": 72,
"start_col": 0,
"start_line": 71
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt8.t -> FStar.BitVector.bv_t 8 | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt8.t",
"LowParse.Spec.BitVector.of_uint8",
"FStar.BitVector.bv_t"
] | [] | false | false | false | false | false | let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
| of_uint8 8 x | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.synth_bv8_inverse | val synth_bv8_inverse:squash (synth_inverse synth_bv8 synth_bv8_recip) | val synth_bv8_inverse:squash (synth_inverse synth_bv8 synth_bv8_recip) | let synth_bv8_inverse : squash (synth_inverse synth_bv8 synth_bv8_recip) =
synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x ->
of_uint8_to_uint8 x
) | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 85,
"start_col": 0,
"start_line": 82
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x
let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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.squash (LowParse.Spec.Combinators.synth_inverse LowParse.Spec.BitVector.synth_bv8
LowParse.Spec.BitVector.synth_bv8_recip) | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Combinators.synth_inverse_intro'",
"FStar.UInt8.t",
"FStar.BitVector.bv_t",
"LowParse.Spec.BitVector.synth_bv8",
"LowParse.Spec.BitVector.synth_bv8_recip",
"LowParse.Spec.BitVector.of_uint8_to_uint8",
"Prims.unit"
] | [] | false | false | true | false | false | let synth_bv8_inverse:squash (synth_inverse synth_bv8 synth_bv8_recip) =
| synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x -> of_uint8_to_uint8 x) | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.parse_bv8 | val parse_bv8:parser parse_bv8_kind (BV.bv_t 8) | val parse_bv8:parser parse_bv8_kind (BV.bv_t 8) | let parse_bv8 : parser parse_bv8_kind (BV.bv_t 8) =
parse_u8 `parse_synth` synth_bv8 | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 34,
"end_line": 90,
"start_col": 0,
"start_line": 89
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x
let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
)
let synth_bv8_inverse : squash (synth_inverse synth_bv8 synth_bv8_recip) =
synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x ->
of_uint8_to_uint8 x
)
let parse_bv8_kind = parse_u8_kind | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | LowParse.Spec.Base.parser LowParse.Spec.BitVector.parse_bv8_kind (FStar.BitVector.bv_t 8) | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Combinators.parse_synth",
"LowParse.Spec.Int.parse_u8_kind",
"FStar.UInt8.t",
"FStar.BitVector.bv_t",
"LowParse.Spec.Int.parse_u8",
"LowParse.Spec.BitVector.synth_bv8"
] | [] | false | false | false | false | false | let parse_bv8:parser parse_bv8_kind (BV.bv_t 8) =
| parse_u8 `parse_synth` synth_bv8 | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.serialize_bv8 | val serialize_bv8:serializer parse_bv8 | val serialize_bv8:serializer parse_bv8 | let serialize_bv8 : serializer parse_bv8 =
serialize_synth
parse_u8
synth_bv8
serialize_u8
synth_bv8_recip
() | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 6,
"end_line": 98,
"start_col": 0,
"start_line": 92
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x
let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
)
let synth_bv8_inverse : squash (synth_inverse synth_bv8 synth_bv8_recip) =
synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x ->
of_uint8_to_uint8 x
)
let parse_bv8_kind = parse_u8_kind
let parse_bv8 : parser parse_bv8_kind (BV.bv_t 8) =
parse_u8 `parse_synth` synth_bv8 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | LowParse.Spec.Base.serializer LowParse.Spec.BitVector.parse_bv8 | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Combinators.serialize_synth",
"LowParse.Spec.Int.parse_u8_kind",
"FStar.UInt8.t",
"FStar.BitVector.bv_t",
"LowParse.Spec.Int.parse_u8",
"LowParse.Spec.BitVector.synth_bv8",
"LowParse.Spec.Int.serialize_u8",
"LowParse.Spec.BitVector.synth_bv8_recip"
] | [] | false | false | false | false | false | let serialize_bv8:serializer parse_bv8 =
| serialize_synth parse_u8 synth_bv8 serialize_u8 synth_bv8_recip () | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.parse_bounded_bv_kind | val parse_bounded_bv_kind (min: nat) (max: nat{min <= max}) (hk: parser_kind) : Tot parser_kind | val parse_bounded_bv_kind (min: nat) (max: nat{min <= max}) (hk: parser_kind) : Tot parser_kind | let parse_bounded_bv_kind
(min: nat)
(max: nat { min <= max })
(hk: parser_kind)
: Tot parser_kind
= hk `and_then_kind` parse_bounded_bv_payload_kind min max | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 58,
"end_line": 285,
"start_col": 0,
"start_line": 280
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x
let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
)
let synth_bv8_inverse : squash (synth_inverse synth_bv8 synth_bv8_recip) =
synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x ->
of_uint8_to_uint8 x
)
let parse_bv8_kind = parse_u8_kind
let parse_bv8 : parser parse_bv8_kind (BV.bv_t 8) =
parse_u8 `parse_synth` synth_bv8
let serialize_bv8 : serializer parse_bv8 =
serialize_synth
parse_u8
synth_bv8
serialize_u8
synth_bv8_recip
()
(* parse a 8*n bit vector *)
let synth_byte_bv (n: nat) (x: (BV.bv_t 8 & BV.bv_t (8 * n))) : Tot (BV.bv_t (8 * (1 + n))) =
Seq.append (fst x) (snd x)
let synth_byte_bv_recip (n: nat) (x: BV.bv_t (8 * (1 + n))) : Tot (BV.bv_t 8 & BV.bv_t (8 * n)) =
Seq.slice x 0 8, Seq.slice x 8 (Seq.length x)
let synth_byte_bv_injective (n: nat) : Lemma
(synth_injective (synth_byte_bv n))
[SMTPat (synth_injective (synth_byte_bv n))]
= synth_inverse_intro' (synth_byte_bv_recip n) (synth_byte_bv n) (fun x ->
let (hd, tl) = x in
let (hd', tl') = synth_byte_bv_recip n (synth_byte_bv n x) in
assert (hd `Seq.equal` hd');
assert (tl `Seq.equal` tl')
)
let synth_byte_bv_inverse (n: nat) : Lemma
(synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))
[SMTPat (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))]
= synth_inverse_intro' (synth_byte_bv n) (synth_byte_bv_recip n) (fun x ->
assert (synth_byte_bv n (synth_byte_bv_recip n x) `Seq.equal` x)
)
let rec parse_byte_bv_kind' (n: nat) : Tot parser_kind =
if n = 0
then parse_ret_kind
else parse_bv8_kind `and_then_kind` parse_byte_bv_kind' (n - 1)
let parse_byte_bv_kind (n: nat) : Tot parser_kind =
strong_parser_kind n n (Some ParserKindMetadataTotal)
let rec parse_byte_bv_kind_eq (n: nat) : Lemma
(parse_byte_bv_kind n == parse_byte_bv_kind' n)
= if n = 0
then ()
else parse_byte_bv_kind_eq (n - 1)
let rec parse_byte_bv (n: nat) : Tot (parser (parse_byte_bv_kind n) (BV.bv_t (8 * n))) =
parse_byte_bv_kind_eq n;
if n = 0
then
parse_ret Seq.empty
else
(parse_bv8 `nondep_then` parse_byte_bv (n - 1)) `parse_synth` synth_byte_bv (n - 1)
let rec serialize_byte_bv (n: nat) : Tot (serializer (parse_byte_bv n)) =
parse_byte_bv_kind_eq n;
if n = 0
then
serialize_ret Seq.empty (fun (x: BV.bv_t 0) -> assert (x `Seq.equal` Seq.empty))
else
serialize_synth
(parse_bv8 `nondep_then` parse_byte_bv (n - 1))
(synth_byte_bv (n - 1))
(serialize_bv8 `serialize_nondep_then` serialize_byte_bv (n - 1))
(synth_byte_bv_recip (n - 1))
()
(* parse extra bits (big-endian with the first bits equal to 0) *)
let extra_bytes_prop (n: nat) (x: U8.t) : Tot bool =
U8.v x < pow2 (n % 8)
let synth_extra_bv8 (n: nat) (x: parse_filter_refine (extra_bytes_prop n)) : Tot (BV.bv_t (n % 8)) =
of_uint8 (n % 8) x
let synth_extra_bv8_recip (n: nat) (x: BV.bv_t (n % 8)) : Tot (parse_filter_refine (extra_bytes_prop n)) =
to_uint8 x
let synth_extra_bv8_injective (n: nat) : Lemma (synth_injective (synth_extra_bv8 n))
[SMTPat (synth_injective (synth_extra_bv8 n))]
=
synth_inverse_intro' (synth_extra_bv8_recip n) (synth_extra_bv8 n) (fun x ->
to_uint8_of_uint8 (n % 8) x
)
let synth_extra_bv8_inverse (n: nat) : Lemma (synth_inverse (synth_extra_bv8 n) (synth_extra_bv8_recip n))
[SMTPat (synth_inverse (synth_extra_bv8 n) (synth_extra_bv8_recip n))]
=
synth_inverse_intro' (synth_extra_bv8 n) (synth_extra_bv8_recip n) (fun x ->
of_uint8_to_uint8 x
)
let parse_extra_bv8_kind (n: nat) : Tot parser_kind =
parse_filter_kind parse_u8_kind
let parse_extra_bv8 (n: nat) : Tot (parser (parse_extra_bv8_kind n) (BV.bv_t (n % 8))) =
(parse_u8 `parse_filter` extra_bytes_prop n) `parse_synth` synth_extra_bv8 n
let serialize_extra_bv8 (n: nat) : Tot (serializer (parse_extra_bv8 n)) =
serialize_synth
_
(synth_extra_bv8 n)
(serialize_filter serialize_u8 (extra_bytes_prop n))
(synth_extra_bv8_recip n)
()
(* parse a bitvector, general *)
let synth_bv (n: nat) (x: (BV.bv_t (n % 8) & BV.bv_t (8 * (n / 8)))) : Tot (BV.bv_t n) =
Seq.append (fst x) (snd x)
let synth_bv_recip (n: nat) (x: BV.bv_t n) : Tot (BV.bv_t (n % 8) & BV.bv_t (8 * (n / 8))) =
Seq.slice x 0 (n % 8), Seq.slice x (n % 8) (Seq.length x)
let synth_bv_injective (n: nat) : Lemma
(synth_injective (synth_bv n))
[SMTPat (synth_injective (synth_bv n))]
= synth_inverse_intro' (synth_bv_recip n) (synth_bv n) (fun x ->
let (hd, tl) = x in
let (hd', tl') = synth_bv_recip n (synth_bv n x) in
assert (hd `Seq.equal` hd');
assert (tl `Seq.equal` tl')
)
let synth_bv_inverse (n: nat) : Lemma
(synth_inverse (synth_bv n) (synth_bv_recip n))
[SMTPat (synth_inverse (synth_bv n) (synth_bv_recip n))]
= synth_inverse_intro' (synth_bv n) (synth_bv_recip n) (fun x ->
assert (synth_bv n (synth_bv_recip n x) `Seq.equal` x)
)
let parse_bv_kind (n: nat) : Tot parser_kind =
if n % 8 = 0
then strong_parser_kind (n / 8) (n / 8) (Some ParserKindMetadataTotal)
else strong_parser_kind (1 + n / 8) (1 + n / 8) None
let parse_bv (n: nat) : Tot (parser (parse_bv_kind n) (BV.bv_t n)) =
if n % 8 = 0 then parse_byte_bv (n / 8) else ((parse_extra_bv8 n `nondep_then` parse_byte_bv (n / 8)) `parse_synth` synth_bv n)
let serialize_bv (n: nat) : Tot (serializer (parse_bv n)) =
if n % 8 = 0
then serialize_byte_bv (n / 8)
else
serialize_synth
_
(synth_bv n)
(serialize_extra_bv8 n `serialize_nondep_then` serialize_byte_bv (n / 8))
(synth_bv_recip n)
()
(* parse a bounded bit vector *)
module U32 = FStar.UInt32
open LowParse.Spec.BoundedInt
inline_for_extraction
let bounded_bv_t
(min: nat)
(max: nat { min <= max })
: Tot Type
= (bitsize: bounded_int32 min max & BV.bv_t (U32.v bitsize))
let parse_bounded_bv_payload_kind
(min: nat)
(max: nat { min <= max })
: Tot parser_kind
= strong_parser_kind
(if min % 8 = 0 then min / 8 else 1 + min / 8)
(if max % 8 = 0 then max / 8 else 1 + max / 8)
None
let parse_bounded_bv_payload_kind_is_weaker_than_parse_bv_kind
(min: nat)
(max: nat)
(n: nat)
: Lemma
(requires (min <= n /\ n <= max))
(ensures (
min <= n /\
n <= max /\
parse_bounded_bv_payload_kind min max `is_weaker_than` parse_bv_kind n
))
[SMTPat (parse_bounded_bv_payload_kind min max `is_weaker_than` parse_bv_kind n)]
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.BoundedInt",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | min: Prims.nat -> max: Prims.nat{min <= max} -> hk: LowParse.Spec.Base.parser_kind
-> LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Combinators.and_then_kind",
"LowParse.Spec.BitVector.parse_bounded_bv_payload_kind"
] | [] | false | false | false | false | false | let parse_bounded_bv_kind (min: nat) (max: nat{min <= max}) (hk: parser_kind) : Tot parser_kind =
| hk `and_then_kind` (parse_bounded_bv_payload_kind min max) | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.parse_byte_bv_kind' | val parse_byte_bv_kind' (n: nat) : Tot parser_kind | val parse_byte_bv_kind' (n: nat) : Tot parser_kind | let rec parse_byte_bv_kind' (n: nat) : Tot parser_kind =
if n = 0
then parse_ret_kind
else parse_bv8_kind `and_then_kind` parse_byte_bv_kind' (n - 1) | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 65,
"end_line": 129,
"start_col": 0,
"start_line": 126
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x
let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
)
let synth_bv8_inverse : squash (synth_inverse synth_bv8 synth_bv8_recip) =
synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x ->
of_uint8_to_uint8 x
)
let parse_bv8_kind = parse_u8_kind
let parse_bv8 : parser parse_bv8_kind (BV.bv_t 8) =
parse_u8 `parse_synth` synth_bv8
let serialize_bv8 : serializer parse_bv8 =
serialize_synth
parse_u8
synth_bv8
serialize_u8
synth_bv8_recip
()
(* parse a 8*n bit vector *)
let synth_byte_bv (n: nat) (x: (BV.bv_t 8 & BV.bv_t (8 * n))) : Tot (BV.bv_t (8 * (1 + n))) =
Seq.append (fst x) (snd x)
let synth_byte_bv_recip (n: nat) (x: BV.bv_t (8 * (1 + n))) : Tot (BV.bv_t 8 & BV.bv_t (8 * n)) =
Seq.slice x 0 8, Seq.slice x 8 (Seq.length x)
let synth_byte_bv_injective (n: nat) : Lemma
(synth_injective (synth_byte_bv n))
[SMTPat (synth_injective (synth_byte_bv n))]
= synth_inverse_intro' (synth_byte_bv_recip n) (synth_byte_bv n) (fun x ->
let (hd, tl) = x in
let (hd', tl') = synth_byte_bv_recip n (synth_byte_bv n x) in
assert (hd `Seq.equal` hd');
assert (tl `Seq.equal` tl')
)
let synth_byte_bv_inverse (n: nat) : Lemma
(synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))
[SMTPat (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))]
= synth_inverse_intro' (synth_byte_bv n) (synth_byte_bv_recip n) (fun x ->
assert (synth_byte_bv n (synth_byte_bv_recip n x) `Seq.equal` x)
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | n: Prims.nat -> LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.op_Equality",
"Prims.int",
"LowParse.Spec.Combinators.parse_ret_kind",
"Prims.bool",
"LowParse.Spec.Combinators.and_then_kind",
"LowParse.Spec.BitVector.parse_bv8_kind",
"LowParse.Spec.BitVector.parse_byte_bv_kind'",
"Prims.op_Subtraction",
"LowParse.Spec.Base.parser_kind"
] | [
"recursion"
] | false | false | false | true | false | let rec parse_byte_bv_kind' (n: nat) : Tot parser_kind =
| if n = 0 then parse_ret_kind else parse_bv8_kind `and_then_kind` (parse_byte_bv_kind' (n - 1)) | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.synth_byte_bv_inverse | val synth_byte_bv_inverse (n: nat)
: Lemma (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))
[SMTPat (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))] | val synth_byte_bv_inverse (n: nat)
: Lemma (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))
[SMTPat (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))] | let synth_byte_bv_inverse (n: nat) : Lemma
(synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))
[SMTPat (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))]
= synth_inverse_intro' (synth_byte_bv n) (synth_byte_bv_recip n) (fun x ->
assert (synth_byte_bv n (synth_byte_bv_recip n x) `Seq.equal` x)
) | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 124,
"start_col": 0,
"start_line": 119
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x
let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
)
let synth_bv8_inverse : squash (synth_inverse synth_bv8 synth_bv8_recip) =
synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x ->
of_uint8_to_uint8 x
)
let parse_bv8_kind = parse_u8_kind
let parse_bv8 : parser parse_bv8_kind (BV.bv_t 8) =
parse_u8 `parse_synth` synth_bv8
let serialize_bv8 : serializer parse_bv8 =
serialize_synth
parse_u8
synth_bv8
serialize_u8
synth_bv8_recip
()
(* parse a 8*n bit vector *)
let synth_byte_bv (n: nat) (x: (BV.bv_t 8 & BV.bv_t (8 * n))) : Tot (BV.bv_t (8 * (1 + n))) =
Seq.append (fst x) (snd x)
let synth_byte_bv_recip (n: nat) (x: BV.bv_t (8 * (1 + n))) : Tot (BV.bv_t 8 & BV.bv_t (8 * n)) =
Seq.slice x 0 8, Seq.slice x 8 (Seq.length x)
let synth_byte_bv_injective (n: nat) : Lemma
(synth_injective (synth_byte_bv n))
[SMTPat (synth_injective (synth_byte_bv n))]
= synth_inverse_intro' (synth_byte_bv_recip n) (synth_byte_bv n) (fun x ->
let (hd, tl) = x in
let (hd', tl') = synth_byte_bv_recip n (synth_byte_bv n x) in
assert (hd `Seq.equal` hd');
assert (tl `Seq.equal` tl')
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | n: Prims.nat
-> FStar.Pervasives.Lemma
(ensures
LowParse.Spec.Combinators.synth_inverse (LowParse.Spec.BitVector.synth_byte_bv n)
(LowParse.Spec.BitVector.synth_byte_bv_recip n))
[
SMTPat (LowParse.Spec.Combinators.synth_inverse (LowParse.Spec.BitVector.synth_byte_bv n)
(LowParse.Spec.BitVector.synth_byte_bv_recip n))
] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"LowParse.Spec.Combinators.synth_inverse_intro'",
"FStar.Pervasives.Native.tuple2",
"FStar.BitVector.bv_t",
"FStar.Mul.op_Star",
"FStar.Seq.Base.seq",
"Prims.bool",
"Prims.b2t",
"Prims.op_Equality",
"FStar.Seq.Base.length",
"Prims.op_Addition",
"LowParse.Spec.BitVector.synth_byte_bv",
"LowParse.Spec.BitVector.synth_byte_bv_recip",
"Prims._assert",
"FStar.Seq.Base.equal",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"LowParse.Spec.Combinators.synth_inverse",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [] | false | false | true | false | false | let synth_byte_bv_inverse (n: nat)
: Lemma (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))
[SMTPat (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))] =
| synth_inverse_intro' (synth_byte_bv n)
(synth_byte_bv_recip n)
(fun x -> assert ((synth_byte_bv n (synth_byte_bv_recip n x)) `Seq.equal` x)) | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.parse_byte_bv_kind_eq | val parse_byte_bv_kind_eq (n: nat) : Lemma (parse_byte_bv_kind n == parse_byte_bv_kind' n) | val parse_byte_bv_kind_eq (n: nat) : Lemma (parse_byte_bv_kind n == parse_byte_bv_kind' n) | let rec parse_byte_bv_kind_eq (n: nat) : Lemma
(parse_byte_bv_kind n == parse_byte_bv_kind' n)
= if n = 0
then ()
else parse_byte_bv_kind_eq (n - 1) | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 36,
"end_line": 138,
"start_col": 0,
"start_line": 134
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x
let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
)
let synth_bv8_inverse : squash (synth_inverse synth_bv8 synth_bv8_recip) =
synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x ->
of_uint8_to_uint8 x
)
let parse_bv8_kind = parse_u8_kind
let parse_bv8 : parser parse_bv8_kind (BV.bv_t 8) =
parse_u8 `parse_synth` synth_bv8
let serialize_bv8 : serializer parse_bv8 =
serialize_synth
parse_u8
synth_bv8
serialize_u8
synth_bv8_recip
()
(* parse a 8*n bit vector *)
let synth_byte_bv (n: nat) (x: (BV.bv_t 8 & BV.bv_t (8 * n))) : Tot (BV.bv_t (8 * (1 + n))) =
Seq.append (fst x) (snd x)
let synth_byte_bv_recip (n: nat) (x: BV.bv_t (8 * (1 + n))) : Tot (BV.bv_t 8 & BV.bv_t (8 * n)) =
Seq.slice x 0 8, Seq.slice x 8 (Seq.length x)
let synth_byte_bv_injective (n: nat) : Lemma
(synth_injective (synth_byte_bv n))
[SMTPat (synth_injective (synth_byte_bv n))]
= synth_inverse_intro' (synth_byte_bv_recip n) (synth_byte_bv n) (fun x ->
let (hd, tl) = x in
let (hd', tl') = synth_byte_bv_recip n (synth_byte_bv n x) in
assert (hd `Seq.equal` hd');
assert (tl `Seq.equal` tl')
)
let synth_byte_bv_inverse (n: nat) : Lemma
(synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))
[SMTPat (synth_inverse (synth_byte_bv n) (synth_byte_bv_recip n))]
= synth_inverse_intro' (synth_byte_bv n) (synth_byte_bv_recip n) (fun x ->
assert (synth_byte_bv n (synth_byte_bv_recip n x) `Seq.equal` x)
)
let rec parse_byte_bv_kind' (n: nat) : Tot parser_kind =
if n = 0
then parse_ret_kind
else parse_bv8_kind `and_then_kind` parse_byte_bv_kind' (n - 1)
let parse_byte_bv_kind (n: nat) : Tot parser_kind =
strong_parser_kind n n (Some ParserKindMetadataTotal) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | n: Prims.nat
-> FStar.Pervasives.Lemma
(ensures
LowParse.Spec.BitVector.parse_byte_bv_kind n == LowParse.Spec.BitVector.parse_byte_bv_kind' n) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.op_Equality",
"Prims.int",
"Prims.bool",
"LowParse.Spec.BitVector.parse_byte_bv_kind_eq",
"Prims.op_Subtraction",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.BitVector.parse_byte_bv_kind",
"LowParse.Spec.BitVector.parse_byte_bv_kind'",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec parse_byte_bv_kind_eq (n: nat) : Lemma (parse_byte_bv_kind n == parse_byte_bv_kind' n) =
| if n = 0 then () else parse_byte_bv_kind_eq (n - 1) | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.synth_bv8_injective | val synth_bv8_injective:squash (synth_injective synth_bv8) | val synth_bv8_injective:squash (synth_injective synth_bv8) | let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
) | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 80,
"start_col": 0,
"start_line": 77
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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.squash (LowParse.Spec.Combinators.synth_injective LowParse.Spec.BitVector.synth_bv8) | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Combinators.synth_inverse_intro'",
"FStar.BitVector.bv_t",
"FStar.UInt8.t",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt8.v",
"Prims.pow2",
"LowParse.Spec.BitVector.synth_bv8_recip",
"LowParse.Spec.BitVector.synth_bv8",
"LowParse.Spec.BitVector.to_uint8_of_uint8",
"Prims.unit"
] | [] | false | false | true | false | false | let synth_bv8_injective:squash (synth_injective synth_bv8) =
| synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x -> to_uint8_of_uint8 8 x) | false |
LowParse.Spec.BitVector.fst | LowParse.Spec.BitVector.synth_byte_bv_recip | val synth_byte_bv_recip (n: nat) (x: BV.bv_t (8 * (1 + n))) : Tot (BV.bv_t 8 & BV.bv_t (8 * n)) | val synth_byte_bv_recip (n: nat) (x: BV.bv_t (8 * (1 + n))) : Tot (BV.bv_t 8 & BV.bv_t (8 * n)) | let synth_byte_bv_recip (n: nat) (x: BV.bv_t (8 * (1 + n))) : Tot (BV.bv_t 8 & BV.bv_t (8 * n)) =
Seq.slice x 0 8, Seq.slice x 8 (Seq.length x) | {
"file_name": "src/lowparse/LowParse.Spec.BitVector.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 47,
"end_line": 107,
"start_col": 0,
"start_line": 106
} | module LowParse.Spec.BitVector
open FStar.Mul
module BV = FStar.BitVector
module U8 = FStar.UInt8
module Seq = FStar.Seq
(* Big-endian conversion of a bit vector to a UInt8 *)
let rec to_uint8
(#n: nat { n <= 8 })
(x: BV.bv_t n)
: Tot (y: U8.t { U8.v y < pow2 n })
= if n = 0
then 0uy
else
let hi = to_uint8 #(n - 1) (Seq.slice x 0 (n - 1)) in
let hi' = hi `U8.mul` 2uy in
let (r: U8.t { U8.v r < 2 }) = if Seq.index x (n - 1) then 1uy else 0uy in
hi' `U8.add` r
let rec of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Tot (BV.bv_t n)
= if n = 0
then Seq.empty
else
let hi = of_uint8 (n - 1) (x `U8.div` 2uy) in
Seq.snoc hi (x `U8.rem` 2uy = 1uy)
#push-options "--z3rlimit 32"
let rec to_uint8_of_uint8
(n: nat { n <= 8 })
(x: U8.t { U8.v x < pow2 n })
: Lemma
(to_uint8 (of_uint8 n x) == x)
= if n = 0
then ()
else begin
assert (Seq.slice (of_uint8 n x) 0 (n - 1) `Seq.equal` of_uint8 (n - 1) (x `U8.div` 2uy));
to_uint8_of_uint8 (n - 1) (x `U8.div` 2uy)
end
let rec of_uint8_to_uint8
(#n: nat {n <= 8})
(x: BV.bv_t n)
: Lemma
(of_uint8 n (to_uint8 x) `Seq.equal` x)
= if n = 0
then ()
else begin
let x' = Seq.slice x 0 (n - 1) in
of_uint8_to_uint8 #(n - 1) x' ;
assert (
U8.v (to_uint8 #(n - 1) x') * 2 == U8.v (to_uint8 x) \/
U8.v (to_uint8 #(n - 1) x') * 2 + 1 == U8.v (to_uint8 x)
);
assert (to_uint8 #(n - 1) x' == to_uint8 x `U8.div` 2uy);
()
end
#pop-options
(* parse a 8-bit vector *)
open LowParse.Spec.Combinators
open LowParse.Spec.Int
let synth_bv8 (x: U8.t) : Tot (BV.bv_t 8) =
of_uint8 8 x
let synth_bv8_recip (x: BV.bv_t 8) : Tot U8.t =
to_uint8 x
let synth_bv8_injective : squash (synth_injective synth_bv8) =
synth_inverse_intro' synth_bv8_recip synth_bv8 (fun x ->
to_uint8_of_uint8 8 x
)
let synth_bv8_inverse : squash (synth_inverse synth_bv8 synth_bv8_recip) =
synth_inverse_intro' synth_bv8 synth_bv8_recip (fun x ->
of_uint8_to_uint8 x
)
let parse_bv8_kind = parse_u8_kind
let parse_bv8 : parser parse_bv8_kind (BV.bv_t 8) =
parse_u8 `parse_synth` synth_bv8
let serialize_bv8 : serializer parse_bv8 =
serialize_synth
parse_u8
synth_bv8
serialize_u8
synth_bv8_recip
()
(* parse a 8*n bit vector *)
let synth_byte_bv (n: nat) (x: (BV.bv_t 8 & BV.bv_t (8 * n))) : Tot (BV.bv_t (8 * (1 + n))) =
Seq.append (fst x) (snd x) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Int.fsti.checked",
"LowParse.Spec.Combinators.fsti.checked",
"LowParse.Spec.BoundedInt.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.BitVector.fst"
} | [
{
"abbrev": false,
"full_module": "LowParse.Spec.Int",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Combinators",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": 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 | n: Prims.nat -> x: FStar.BitVector.bv_t (8 * (1 + n))
-> FStar.BitVector.bv_t 8 * FStar.BitVector.bv_t (8 * n) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"FStar.BitVector.bv_t",
"FStar.Mul.op_Star",
"Prims.op_Addition",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Seq.Base.slice",
"Prims.bool",
"FStar.Seq.Base.length",
"FStar.Pervasives.Native.tuple2"
] | [] | false | false | false | false | false | let synth_byte_bv_recip (n: nat) (x: BV.bv_t (8 * (1 + n))) : Tot (BV.bv_t 8 & BV.bv_t (8 * n)) =
| Seq.slice x 0 8, Seq.slice x 8 (Seq.length x) | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.