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.Seq.Permutation.fst
FStar.Seq.Permutation.split3_index
val split3_index (#a: eqtype) (s0: seq a) (x: a) (s1: seq a) (j: nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures (let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1)))
val split3_index (#a: eqtype) (s0: seq a) (x: a) (s1: seq a) (j: nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures (let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1)))
let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else ()
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 11, "end_line": 56, "start_col": 0, "start_line": 44 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s0: FStar.Seq.Base.seq a -> x: a -> s1: FStar.Seq.Base.seq a -> j: Prims.nat -> FStar.Pervasives.Lemma (requires j < FStar.Seq.Base.length (FStar.Seq.Base.append s0 s1)) (ensures (let s = FStar.Seq.Base.append s0 (FStar.Seq.Base.cons x s1) in let s' = FStar.Seq.Base.append s0 s1 in let n = FStar.Seq.Base.length s0 in (match j < n with | true -> FStar.Seq.Base.index s' j == FStar.Seq.Base.index s j | _ -> FStar.Seq.Base.index s' j == FStar.Seq.Base.index s (j + 1)) <: Type0))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.eqtype", "FStar.Seq.Base.seq", "Prims.nat", "Prims.op_LessThan", "Prims.bool", "Prims.unit", "FStar.Seq.Base.length", "FStar.Seq.Base.append", "Prims.b2t", "Prims.squash", "Prims.eq2", "FStar.Seq.Base.index", "Prims.op_Addition", "FStar.Seq.Base.cons", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let split3_index (#a: eqtype) (s0: seq a) (x: a) (s1: seq a) (j: nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures (let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1))) =
let n = Seq.length (Seq.append s0 s1) in if j < n then ()
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.find
val find (#a: eqtype) (x: a) (s: seq a {count x s > 0}) : Tot (frags: (seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` (Seq.append (fst frags) (Seq.cons x (snd frags))) }) (decreases (Seq.length s))
val find (#a: eqtype) (x: a) (s: seq a {count x s > 0}) : Tot (frags: (seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` (Seq.append (fst frags) (Seq.cons x (snd frags))) }) (decreases (Seq.length s))
let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx )
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 74, "start_col": 0, "start_line": 59 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 2, "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": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 2, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: a -> s: FStar.Seq.Base.seq a {FStar.Seq.Properties.count x s > 0} -> Prims.Tot (frags: (FStar.Seq.Base.seq a * FStar.Seq.Base.seq a) { let s' = FStar.Seq.Base.append (FStar.Pervasives.Native.fst frags) (FStar.Pervasives.Native.snd frags) in let n = FStar.Seq.Base.length (FStar.Pervasives.Native.fst frags) in FStar.Seq.Base.equal s (FStar.Seq.Base.append (FStar.Pervasives.Native.fst frags) (FStar.Seq.Base.cons x (FStar.Pervasives.Native.snd frags))) })
Prims.Tot
[ "total", "" ]
[]
[ "Prims.eqtype", "FStar.Seq.Base.seq", "Prims.b2t", "Prims.op_GreaterThan", "FStar.Seq.Properties.count", "Prims.op_Equality", "FStar.Seq.Properties.head", "FStar.Pervasives.Native.Mktuple2", "FStar.Seq.Base.empty", "FStar.Seq.Properties.tail", "Prims.bool", "FStar.Seq.Base.cons", "Prims.unit", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Base.append", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.fst", "FStar.Pervasives.Native.snd", "Prims.nat", "FStar.Seq.Base.length", "FStar.Seq.Permutation.find" ]
[ "recursion" ]
false
false
false
false
false
let rec find (#a: eqtype) (x: a) (s: seq a {count x s > 0}) : Tot (frags: (seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` (Seq.append (fst frags) (Seq.cons x (snd frags))) }) (decreases (Seq.length s)) =
if Seq.head s = x then Seq.empty, Seq.tail s else (let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx)
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.introduce_is_permutation
val introduce_is_permutation: #a: Type -> s0: seq a -> s1: seq a -> f: index_fun s0 -> squash (Seq.length s0 == Seq.length s1) -> squash (forall x y. x <> y ==> f x <> f y) -> squash (forall (i: nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i)) -> Lemma (ensures is_permutation s0 s1 f)
val introduce_is_permutation: #a: Type -> s0: seq a -> s1: seq a -> f: index_fun s0 -> squash (Seq.length s0 == Seq.length s1) -> squash (forall x y. x <> y ==> f x <> f y) -> squash (forall (i: nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i)) -> Lemma (ensures is_permutation s0 s1 f)
let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 41, "end_line": 85, "start_col": 0, "start_line": 77 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s0: FStar.Seq.Base.seq a -> s1: FStar.Seq.Base.seq a -> f: FStar.Seq.Permutation.index_fun s0 -> _: Prims.squash (FStar.Seq.Base.length s0 == FStar.Seq.Base.length s1) -> _: Prims.squash (forall (x: FStar.IntegerIntervals.under (FStar.Seq.Base.length s0)) (y: FStar.IntegerIntervals.under (FStar.Seq.Base.length s0)). x <> y ==> f x <> f y) -> _: Prims.squash (forall (i: Prims.nat{i < FStar.Seq.Base.length s0}). FStar.Seq.Base.index s0 i == FStar.Seq.Base.index s1 (f i)) -> FStar.Pervasives.Lemma (ensures FStar.Seq.Permutation.is_permutation s0 s1 f)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Seq.Base.seq", "FStar.Seq.Permutation.index_fun", "Prims.squash", "Prims.eq2", "Prims.nat", "FStar.Seq.Base.length", "Prims.l_Forall", "FStar.IntegerIntervals.under", "Prims.l_imp", "Prims.b2t", "Prims.op_disEquality", "Prims.op_LessThan", "FStar.Seq.Base.index", "FStar.Seq.Permutation.reveal_is_permutation_nopats", "Prims.unit", "Prims.l_True", "FStar.Seq.Permutation.is_permutation", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let introduce_is_permutation (#a: Type) (s0: seq a) (s1: seq a) (f: index_fun s0) (_: squash (Seq.length s0 == Seq.length s1)) (_: squash (forall x y. x <> y ==> f x <> f y)) (_: squash (forall (i: nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) =
reveal_is_permutation_nopats s0 s1 f
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.witness_exists
val witness_exists (#a:Type) (#opened_invariants:_) (#p:a -> vprop) (_:unit) : SteelGhostT (erased a) opened_invariants (h_exists p) (fun x -> p x)
val witness_exists (#a:Type) (#opened_invariants:_) (#p:a -> vprop) (_:unit) : SteelGhostT (erased a) opened_invariants (h_exists p) (fun x -> p x)
let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x)))
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 79, "end_line": 618, "start_col": 0, "start_line": 617 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: Prims.unit -> Steel.Effect.Atomic.SteelGhostT (FStar.Ghost.erased a)
Steel.Effect.Atomic.SteelGhostT
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Prims.unit", "Steel.Memory.witness_h_exists", "Steel.Effect.Common.hp_of", "Steel.Memory.slprop", "FStar.Ghost.erased", "Steel.Effect.Atomic.h_exists", "FStar.Ghost.reveal" ]
[]
false
true
false
false
false
let witness_exists #a #u #p _ =
SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x)))
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.mk_selector_vprop_sel'
val mk_selector_vprop_sel' (#t: Type) (p: (t -> vprop)) (p_inj: interp_hp_of_injective p) : Tot (selector' t (mk_selector_vprop_hp p))
val mk_selector_vprop_sel' (#t: Type) (p: (t -> vprop)) (p_inj: interp_hp_of_injective p) : Tot (selector' t (mk_selector_vprop_hp p))
let mk_selector_vprop_sel' (#t: Type) (p: t -> vprop) (p_inj: interp_hp_of_injective p) // unused in the definition, but necessary for the local SMTPats below : Tot (selector' t (mk_selector_vprop_hp p)) = fun m -> id_elim_exists (hp_of_pointwise p) m
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 47, "end_line": 882, "start_col": 0, "start_line": 877 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i (* * AR: SteelAtomic and SteelGhost are not marked reifiable since we intend to run Steel programs natively * However to implement the with_inv combinators we need to reify their thunks to reprs * We could implement it better by having support for reification only in the .fst file * But for now assuming a function *) assume val reify_steel_atomic_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#g:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelAtomicBase a framed opened_invariants g pre post req ens) : repr a framed opened_invariants g pre post req ens [@@warn_on_use "as_unobservable_atomic_action is a trusted primitive"] let as_atomic_o_action (#a:Type u#a) (#opened_invariants:inames) (#fp:slprop) (#fp': a -> slprop) (o:observability) (f:action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) = SteelAtomicBaseT?.reflect f let with_invariant #a #fp #fp' #obs #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_o_action obs (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_atomic_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return x assume val reify_steel_ghost_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelGhostBase a framed opened_invariants Unobservable pre post req ens) : repr a framed opened_invariants Unobservable pre post req ens let with_invariant_g #a #fp #fp' #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_unobservable_action (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_ghost_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return (hide x) let intro_vrefine v p = let m = get () in let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased (vrefine_t v p) = Ghost.hide (Ghost.reveal x) in change_slprop v (vrefine v p) x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let elim_vrefine v p = let h = get() in let x : Ghost.erased (vrefine_t v p) = gget (vrefine v p) in let x' : Ghost.erased (t_of v) = Ghost.hide (Ghost.reveal x) in change_slprop (vrefine v p) v x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let vdep_cond (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) : Tot prop = q == p (fst x1) let vdep_rel (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) (x2: (t_of (vdep v p))) : Tot prop = q == p (fst x1) /\ dfst (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == fst x1 /\ dsnd (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == snd x1 let intro_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (v `star` q)) m /\ q == p (fst (sel_of (v `star` q) m)) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let intro_vdep v q p = reveal_star v q; change_slprop_rel_with_cond (v `star` q) (vdep v p) (vdep_cond v q p) (vdep_rel v q p) (fun m -> intro_vdep_lemma v q p m) let vdep_cond_recip (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) (x2: t_of (vdep v p)) : Tot prop = q == p (dfst (x2 <: dtuple2 (t_of v) (vdep_payload v p))) let vdep_rel_recip (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x2: (t_of (vdep v p))) (x1: t_of (v `star` q)) : Tot prop = vdep_rel v q p x1 x2 let elim_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (vdep v p)) m /\ q == p (dfst (sel_of (vdep v p) m <: dtuple2 (t_of v) (vdep_payload v p))) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let elim_vdep0 (#opened:inames) (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) : SteelGhost unit opened (vdep v p) (fun _ -> v `star` q) (requires (fun h -> q == p (dfst (h (vdep v p))))) (ensures (fun h _ h' -> let fs = h' v in let sn = h' q in let x2 = h (vdep v p) in q == p fs /\ dfst x2 == fs /\ dsnd x2 == sn )) = change_slprop_rel_with_cond (vdep v p) (v `star` q) (vdep_cond_recip v p q) (vdep_rel_recip v q p) (fun m -> elim_vdep_lemma v q p m); reveal_star v q let elim_vdep v p = let r = gget (vdep v p) in let res = Ghost.hide (dfst #(t_of v) #(vdep_payload v p) (Ghost.reveal r)) in elim_vdep0 v p (p (Ghost.reveal res)); res let intro_vrewrite v #t f = let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased t = Ghost.hide (f (Ghost.reveal x)) in change_slprop v (vrewrite v f) x x' (fun m -> vrewrite_sel_eq v f m ) let elim_vrewrite v #t f = change_slprop_rel (vrewrite v f) v (fun y x -> y == f x) (fun m -> vrewrite_sel_eq v f m) /// Deriving a selector-style vprop from an injective pts-to-style vprop let hp_of_pointwise (#t: Type) (p: t -> vprop) (x: t) : Tot slprop = hp_of (p x) let mk_selector_vprop_hp p = Steel.Memory.h_exists (hp_of_pointwise p)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: (_: t -> Steel.Effect.Common.vprop) -> p_inj: Steel.Effect.Atomic.interp_hp_of_injective p -> Steel.Effect.Common.selector' t (Steel.Effect.Atomic.mk_selector_vprop_hp p)
Prims.Tot
[ "total" ]
[]
[ "Steel.Effect.Common.vprop", "Steel.Effect.Atomic.interp_hp_of_injective", "Steel.Memory.hmem", "Steel.Effect.Atomic.mk_selector_vprop_hp", "FStar.Ghost.reveal", "Steel.Memory.id_elim_exists", "Steel.Effect.Atomic.hp_of_pointwise", "Steel.Effect.Common.selector'" ]
[]
false
false
false
false
false
let mk_selector_vprop_sel' (#t: Type) (p: (t -> vprop)) (p_inj: interp_hp_of_injective p) : Tot (selector' t (mk_selector_vprop_hp p)) =
fun m -> id_elim_exists (hp_of_pointwise p) m
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc_singleton
val foldm_snoc_singleton (#a:_) (#eq:_) (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x)
val foldm_snoc_singleton (#a:_) (#eq:_) (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x)
let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 274, "start_col": 0, "start_line": 272 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "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
m: FStar.Algebra.CommMonoid.Equiv.cm a eq -> x: a -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc m (FStar.Seq.Base.create 1 x)) x)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Seq.Permutation.elim_monoid_laws", "Prims.unit", "Prims.l_True", "Prims.squash", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Seq.Permutation.foldm_snoc", "FStar.Seq.Base.create", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let foldm_snoc_singleton (#a: _) #eq (m: CE.cm a eq) (x: a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) =
elim_monoid_laws m
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc_unit_seq
val foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit)
val foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit)
let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 35, "end_line": 269, "start_col": 0, "start_line": 260 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: FStar.Algebra.CommMonoid.Equiv.cm a eq -> s: FStar.Seq.Base.seq a -> FStar.Pervasives.Lemma (requires FStar.Seq.Base.equal s (FStar.Seq.Base.create (FStar.Seq.Base.length s) (CM?.unit m))) (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc m s) (CM?.unit m)) (decreases FStar.Seq.Base.length s)
FStar.Pervasives.Lemma
[ "", "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Seq.Base.seq", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "Prims.bool", "FStar.Seq.Permutation.foldm_snoc_unit_seq", "Prims.unit", "FStar.Pervasives.Native.tuple2", "Prims.eq2", "FStar.Seq.Properties.snoc", "FStar.Pervasives.Native.fst", "FStar.Pervasives.Native.snd", "FStar.Seq.Properties.un_snoc", "FStar.Seq.Permutation.elim_monoid_laws", "FStar.Algebra.CommMonoid.Equiv.elim_eq_laws", "FStar.Seq.Base.equal", "FStar.Seq.Base.create", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit", "Prims.squash", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Seq.Permutation.foldm_snoc", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec foldm_snoc_unit_seq (#a: Type) (#eq: CE.equiv a) (m: CE.cm a eq) (s: Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) =
CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.elim_eq_laws
val elim_eq_laws (#a: _) (eq: CE.equiv a) : Lemma ((forall x. {:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y. {:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z. {:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z))
val elim_eq_laws (#a: _) (eq: CE.equiv a) : Lemma ((forall x. {:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y. {:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z. {:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z))
let elim_eq_laws #a (eq:CE.equiv a) : Lemma ( (forall x.{:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y.{:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z.{:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z) ) = CE.elim_eq_laws eq
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 23, "end_line": 500, "start_col": 0, "start_line": 494 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *) #push-options "--z3rlimit_factor 2 --fuel 1 --ifuel 0" let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0)) #pop-options //////////////////////////////////////////////////////////////////////////////// // foldm_snoc_split //////////////////////////////////////////////////////////////////////////////// (* Some utilities to introduce associativity-commutativity reasoning on CM using quantified formulas with patterns. Use these with care, since with large terms the SMT solver may end up with an explosion of instantiations *) let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y z:c). {:pattern (x `cm.mult` y `cm.mult` z)} (x `cm.mult` y `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) = Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity) let cm_commutativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y:c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x)) = Classical.forall_intro_2 (Classical.move_requires_2 cm.commutativity) (* A utility to introduce the equivalence relation laws into the context. FStar.Algebra.CommutativeMonoid provides something similar, but this version provides a more goal-directed pattern for transitivity.
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
eq: FStar.Algebra.CommMonoid.Equiv.equiv a -> FStar.Pervasives.Lemma (ensures (forall (x: a). {:pattern EQ?.eq eq x x} EQ?.eq eq x x) /\ (forall (x: a) (y: a). {:pattern EQ?.eq eq x y} EQ?.eq eq x y ==> EQ?.eq eq y x) /\ (forall (x: a) (y: a) (z: a). {:pattern EQ?.eq eq x y; EQ?.eq eq x z} EQ?.eq eq x y /\ EQ?.eq eq y z ==> EQ?.eq eq x z))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.elim_eq_laws", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.l_and", "Prims.l_Forall", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "Prims.l_imp", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let elim_eq_laws #a (eq: CE.equiv a) : Lemma ((forall x. {:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y. {:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z. {:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z)) =
CE.elim_eq_laws eq
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc_sym
val foldm_snoc_sym (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1)))
val foldm_snoc_sym (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1)))
let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 331, "start_col": 0, "start_line": 325 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: FStar.Algebra.CommMonoid.Equiv.cm a eq -> s1: FStar.Seq.Base.seq a -> s2: FStar.Seq.Base.seq a -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc m (FStar.Seq.Base.append s1 s2)) (FStar.Seq.Permutation.foldm_snoc m (FStar.Seq.Base.append s2 s1)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Seq.Base.seq", "FStar.Seq.Permutation.foldm_snoc_append", "Prims.unit", "FStar.Seq.Permutation.elim_monoid_laws", "FStar.Algebra.CommMonoid.Equiv.elim_eq_laws", "Prims.l_True", "Prims.squash", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Seq.Permutation.foldm_snoc", "FStar.Seq.Base.append", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let foldm_snoc_sym #a #eq (m: CE.cm a eq) (s1: seq a) (s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) =
CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.x_yz_to_y_xz
val x_yz_to_y_xz (#a #eq: _) (m: CE.cm a eq) (x y z: a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z)))
val x_yz_to_y_xz (#a #eq: _) (m: CE.cm a eq) (x y z: a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z)))
let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); }
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 291, "start_col": 0, "start_line": 277 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: FStar.Algebra.CommMonoid.Equiv.cm a eq -> x: a -> y: a -> z: a -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (CM?.mult m x (CM?.mult m y z)) (CM?.mult m y (CM?.mult m x z)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Calc.calc_finish", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__commutativity", "Prims.squash", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__associativity", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__congruence", "FStar.Seq.Permutation.elim_monoid_laws", "FStar.Algebra.CommMonoid.Equiv.elim_eq_laws", "Prims.l_True", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let x_yz_to_y_xz #a #eq (m: CE.cm a eq) (x: a) (y: a) (z: a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) =
CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); }
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.remove_i
val remove_i (#a: _) (s: seq a) (i: nat{i < Seq.length s}) : a & seq a
val remove_i (#a: _) (s: seq a) (i: nat{i < Seq.length s}) : a & seq a
let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1)
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 375, "start_col": 0, "start_line": 372 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: FStar.Seq.Base.seq a -> i: Prims.nat{i < FStar.Seq.Base.length s} -> a * FStar.Seq.Base.seq a
Prims.Tot
[ "total" ]
[]
[ "FStar.Seq.Base.seq", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.Seq.Base.length", "FStar.Pervasives.Native.Mktuple2", "FStar.Seq.Properties.head", "FStar.Seq.Base.append", "FStar.Seq.Properties.tail", "FStar.Pervasives.Native.tuple2", "FStar.Seq.Properties.split" ]
[]
false
false
false
false
false
let remove_i #a (s: seq a) (i: nat{i < Seq.length s}) : a & seq a =
let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1)
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.reveal_star_30
val reveal_star_30 (#opened: inames) (p1 p2 p3: vprop) : repr unit false opened Unobservable ((p1 `star` p2) `star` p3) (fun _ -> (p1 `star` p2) `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split ((p1 `star` p2) `star` p3) p1 /\ can_be_split ((p1 `star` p2) `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 ((p1 `star` p2) `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 ((p1 `star` p2) `star` p3) == ((h1 p1, h1 p2), h1 p3))
val reveal_star_30 (#opened: inames) (p1 p2 p3: vprop) : repr unit false opened Unobservable ((p1 `star` p2) `star` p3) (fun _ -> (p1 `star` p2) `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split ((p1 `star` p2) `star` p3) p1 /\ can_be_split ((p1 `star` p2) `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 ((p1 `star` p2) `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 ((p1 `star` p2) `star` p3) == ((h1 p1, h1 p2), h1 p3))
let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 49, "end_line": 594, "start_col": 0, "start_line": 576 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p1: Steel.Effect.Common.vprop -> p2: Steel.Effect.Common.vprop -> p3: Steel.Effect.Common.vprop -> Steel.Effect.Atomic.repr Prims.unit false opened Steel.Effect.Common.Unobservable (Steel.Effect.Common.star (Steel.Effect.Common.star p1 p2) p3) (fun _ -> Steel.Effect.Common.star (Steel.Effect.Common.star p1 p2) p3) (fun _ -> Prims.l_True) (fun h0 _ h1 -> Steel.Effect.Common.can_be_split (Steel.Effect.Common.star (Steel.Effect.Common.star p1 p2 ) p3) p1 /\ Steel.Effect.Common.can_be_split (Steel.Effect.Common.star (Steel.Effect.Common.star p1 p2 ) p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (Steel.Effect.Common.star (Steel.Effect.Common.star p1 p2) p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (Steel.Effect.Common.star (Steel.Effect.Common.star p1 p2) p3) == ((h1 p1, h1 p2), h1 p3))
Prims.Tot
[ "total" ]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Memory.slprop", "Steel.Effect.Common.reveal_mk_rmem", "Steel.Effect.Common.star", "Prims.unit", "Steel.Effect.Common.can_be_split_trans", "Steel.Effect.Common.rmem'", "Steel.Effect.Common.valid_rmem", "Steel.Effect.Common.mk_rmem", "Steel.Memory.core_mem", "FStar.Classical.forall_intro_3", "Steel.Effect.Common.hmem", "Steel.Effect.Common.can_be_split", "Prims.eq2", "Steel.Effect.Common.normal", "Steel.Effect.Common.t_of", "Steel.Effect.Common.sel_of", "Steel.Memory.full_mem", "FStar.NMSTTotal.get", "Steel.Memory.mem_evolves", "Steel.Effect.Atomic.repr", "Steel.Effect.Common.Unobservable", "Steel.Effect.Common.rmem", "Prims.l_True", "Prims.l_and", "FStar.Pervasives.Native.tuple2", "Steel.Effect.Common.vprop'", "Steel.Effect.Common.__proj__Mkvprop'__item__t", "FStar.Pervasives.Native.Mktuple2" ]
[]
false
false
false
false
false
let reveal_star_30 (#opened: inames) (p1 p2 p3: vprop) : repr unit false opened Unobservable ((p1 `star` p2) `star` p3) (fun _ -> (p1 `star` p2) `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split ((p1 `star` p2) `star` p3) p1 /\ can_be_split ((p1 `star` p2) `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 ((p1 `star` p2) `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 ((p1 `star` p2) `star` p3) == ((h1 p1, h1 p2), h1 p3)) =
fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem ((p1 `star` p2) `star` p3) (core_mem m) in can_be_split_trans ((p1 `star` p2) `star` p3) (p1 `star` p2) p1; can_be_split_trans ((p1 `star` p2) `star` p3) (p1 `star` p2) p2; reveal_mk_rmem ((p1 `star` p2) `star` p3) m ((p1 `star` p2) `star` p3); reveal_mk_rmem ((p1 `star` p2) `star` p3) m (p1 `star` p2); reveal_mk_rmem ((p1 `star` p2) `star` p3) m p3
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.elim_monoid_laws
val elim_monoid_laws (#a #eq: _) (m: CE.cm a eq) : Lemma ((forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z. {:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x. {:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x))
val elim_monoid_laws (#a #eq: _) (m: CE.cm a eq) : Lemma ((forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z. {:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x. {:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x))
let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x )
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 37, "end_line": 258, "start_col": 0, "start_line": 244 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: FStar.Algebra.CommMonoid.Equiv.cm a eq -> FStar.Pervasives.Lemma (ensures (forall (x: a) (y: a). {:pattern CM?.mult m x y} EQ?.eq eq (CM?.mult m x y) (CM?.mult m y x)) /\ (forall (x: a) (y: a) (z: a). {:pattern CM?.mult m x (CM?.mult m y z)} EQ?.eq eq (CM?.mult m x (CM?.mult m y z)) (CM?.mult m (CM?.mult m x y) z)) /\ (forall (x: a). {:pattern CM?.mult m x (CM?.unit m)} EQ?.eq eq (CM?.mult m x (CM?.unit m)) x))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Classical.Sugar.forall_intro", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit", "FStar.Algebra.CommMonoid.Equiv.right_identity", "Prims.squash", "Prims.unit", "Prims.l_Forall", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__symmetry", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__associativity", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__commutativity", "Prims.l_True", "Prims.l_and", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let elim_monoid_laws #a #eq (m: CE.cm a eq) : Lemma ((forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z. {:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x. {:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x)) =
introduce forall x y . eq.eq (m.mult x y) (m.mult y x) with (m.commutativity x y); introduce forall x y z . eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with (m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z))); introduce forall x . eq.eq (m.mult x m.unit) x with (CE.right_identity eq m x)
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.subcomp_opaque
val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True)
val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True)
let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 5, "end_line": 337, "start_col": 0, "start_line": 304 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> opened: Steel.Memory.inames -> o1: FStar.Pervasives.eqtype_as_type Steel.Effect.Common.observability -> o2: FStar.Pervasives.eqtype_as_type Steel.Effect.Common.observability -> f: Steel.Effect.Atomic.repr a framed_f opened o1 pre_f post_f req_f ens_f -> Prims.Pure (Steel.Effect.Atomic.repr a framed_g opened o2 pre_g post_g req_g ens_g)
Prims.Pure
[]
[]
[ "Steel.Memory.inames", "FStar.Pervasives.eqtype_as_type", "Steel.Effect.Common.observability", "Prims.bool", "Steel.Effect.Common.pre_t", "Steel.Effect.Common.post_t", "Steel.Effect.Common.req_t", "Steel.Effect.Common.ens_t", "Steel.Effect.Common.vprop", "Prims.prop", "Prims.squash", "Steel.Effect.Common.maybe_emp", "Steel.Effect.Common.can_be_split_dep", "Steel.Effect.Common.star", "Steel.Effect.Common.equiv_forall", "Steel.Effect.Atomic.repr", "Steel.Memory.slprop", "Prims.unit", "Steel.Effect.focus_replace", "Steel.Memory.core_mem", "Steel.Effect.can_be_split_3_interp", "Steel.Effect.Common.hp_of", "Steel.Memory.locks_invariant", "Steel.Effect.Common.can_be_split_trans", "Prims._assert", "Steel.Effect.frame_opaque", "Steel.Effect.Common.focus_rmem", "Steel.Effect.Common.rmem'", "Steel.Effect.Common.valid_rmem", "Steel.Effect.Common.mk_rmem", "Steel.Memory.full_mem", "FStar.NMSTTotal.get", "Steel.Memory.mem_evolves", "Steel.Effect.Atomic.frame00" ]
[]
false
false
false
false
false
let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f =
fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem ((post_f x) `star` fr) (core_mem m1) in assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) ((post_f x) `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) ((post_f x) `star` fr) (post_f x); can_be_split_trans (post_g x) ((post_f x) `star` fr) fr; can_be_split_3_interp (hp_of ((post_f x) `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) ((post_f x) `star` fr) (post_f x) (core_mem m1); x
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.mk_selector_vprop_sel
val mk_selector_vprop_sel (#t: Type0) (p: t -> vprop) (p_inj: interp_hp_of_injective p) : Tot (selector t (mk_selector_vprop_hp p))
val mk_selector_vprop_sel (#t: Type0) (p: t -> vprop) (p_inj: interp_hp_of_injective p) : Tot (selector t (mk_selector_vprop_hp p))
let mk_selector_vprop_sel #t p p_inj = let varrayp_sel_depends_only_on (#t: Type) (p: t -> vprop) (p_inj: interp_hp_of_injective p) (m0: Steel.Memory.hmem (mk_selector_vprop_hp p)) (m1: mem { disjoint m0 m1 }) : Lemma ( mk_selector_vprop_sel' p p_inj m0 == mk_selector_vprop_sel' p p_inj (Steel.Memory.join m0 m1) ) [SMTPat (mk_selector_vprop_sel' p p_inj (Steel.Memory.join m0 m1))] = p_inj (mk_selector_vprop_sel' p p_inj m0) (mk_selector_vprop_sel' p p_inj (Steel.Memory.join m0 m1)) (Steel.Memory.join m0 m1) in let varrayp_sel_depends_only_on_core (#t: Type) (p: t -> vprop) (p_inj: interp_hp_of_injective p) (m0: Steel.Memory.hmem (mk_selector_vprop_hp p)) : Lemma ( mk_selector_vprop_sel' p p_inj m0 == mk_selector_vprop_sel' p p_inj (core_mem m0) ) [SMTPat (mk_selector_vprop_sel' p p_inj (core_mem m0))] = p_inj (mk_selector_vprop_sel' p p_inj m0) (mk_selector_vprop_sel' p p_inj (core_mem m0)) m0 in mk_selector_vprop_sel' p p_inj
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 32, "end_line": 912, "start_col": 0, "start_line": 884 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i (* * AR: SteelAtomic and SteelGhost are not marked reifiable since we intend to run Steel programs natively * However to implement the with_inv combinators we need to reify their thunks to reprs * We could implement it better by having support for reification only in the .fst file * But for now assuming a function *) assume val reify_steel_atomic_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#g:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelAtomicBase a framed opened_invariants g pre post req ens) : repr a framed opened_invariants g pre post req ens [@@warn_on_use "as_unobservable_atomic_action is a trusted primitive"] let as_atomic_o_action (#a:Type u#a) (#opened_invariants:inames) (#fp:slprop) (#fp': a -> slprop) (o:observability) (f:action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) = SteelAtomicBaseT?.reflect f let with_invariant #a #fp #fp' #obs #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_o_action obs (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_atomic_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return x assume val reify_steel_ghost_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelGhostBase a framed opened_invariants Unobservable pre post req ens) : repr a framed opened_invariants Unobservable pre post req ens let with_invariant_g #a #fp #fp' #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_unobservable_action (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_ghost_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return (hide x) let intro_vrefine v p = let m = get () in let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased (vrefine_t v p) = Ghost.hide (Ghost.reveal x) in change_slprop v (vrefine v p) x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let elim_vrefine v p = let h = get() in let x : Ghost.erased (vrefine_t v p) = gget (vrefine v p) in let x' : Ghost.erased (t_of v) = Ghost.hide (Ghost.reveal x) in change_slprop (vrefine v p) v x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let vdep_cond (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) : Tot prop = q == p (fst x1) let vdep_rel (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) (x2: (t_of (vdep v p))) : Tot prop = q == p (fst x1) /\ dfst (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == fst x1 /\ dsnd (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == snd x1 let intro_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (v `star` q)) m /\ q == p (fst (sel_of (v `star` q) m)) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let intro_vdep v q p = reveal_star v q; change_slprop_rel_with_cond (v `star` q) (vdep v p) (vdep_cond v q p) (vdep_rel v q p) (fun m -> intro_vdep_lemma v q p m) let vdep_cond_recip (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) (x2: t_of (vdep v p)) : Tot prop = q == p (dfst (x2 <: dtuple2 (t_of v) (vdep_payload v p))) let vdep_rel_recip (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x2: (t_of (vdep v p))) (x1: t_of (v `star` q)) : Tot prop = vdep_rel v q p x1 x2 let elim_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (vdep v p)) m /\ q == p (dfst (sel_of (vdep v p) m <: dtuple2 (t_of v) (vdep_payload v p))) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let elim_vdep0 (#opened:inames) (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) : SteelGhost unit opened (vdep v p) (fun _ -> v `star` q) (requires (fun h -> q == p (dfst (h (vdep v p))))) (ensures (fun h _ h' -> let fs = h' v in let sn = h' q in let x2 = h (vdep v p) in q == p fs /\ dfst x2 == fs /\ dsnd x2 == sn )) = change_slprop_rel_with_cond (vdep v p) (v `star` q) (vdep_cond_recip v p q) (vdep_rel_recip v q p) (fun m -> elim_vdep_lemma v q p m); reveal_star v q let elim_vdep v p = let r = gget (vdep v p) in let res = Ghost.hide (dfst #(t_of v) #(vdep_payload v p) (Ghost.reveal r)) in elim_vdep0 v p (p (Ghost.reveal res)); res let intro_vrewrite v #t f = let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased t = Ghost.hide (f (Ghost.reveal x)) in change_slprop v (vrewrite v f) x x' (fun m -> vrewrite_sel_eq v f m ) let elim_vrewrite v #t f = change_slprop_rel (vrewrite v f) v (fun y x -> y == f x) (fun m -> vrewrite_sel_eq v f m) /// Deriving a selector-style vprop from an injective pts-to-style vprop let hp_of_pointwise (#t: Type) (p: t -> vprop) (x: t) : Tot slprop = hp_of (p x) let mk_selector_vprop_hp p = Steel.Memory.h_exists (hp_of_pointwise p) let mk_selector_vprop_sel' (#t: Type) (p: t -> vprop) (p_inj: interp_hp_of_injective p) // unused in the definition, but necessary for the local SMTPats below : Tot (selector' t (mk_selector_vprop_hp p)) = fun m -> id_elim_exists (hp_of_pointwise p) m
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: (_: t -> Steel.Effect.Common.vprop) -> p_inj: Steel.Effect.Atomic.interp_hp_of_injective p -> Steel.Effect.Common.selector t (Steel.Effect.Atomic.mk_selector_vprop_hp p)
Prims.Tot
[ "total" ]
[]
[ "Steel.Effect.Common.vprop", "Steel.Effect.Atomic.interp_hp_of_injective", "Steel.Effect.Atomic.mk_selector_vprop_sel'", "Steel.Memory.hmem", "Steel.Effect.Atomic.mk_selector_vprop_hp", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "Steel.Memory.core_mem", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil", "Steel.Memory.mem", "Steel.Memory.disjoint", "Steel.Memory.join", "Steel.Effect.Common.selector" ]
[]
false
false
false
false
false
let mk_selector_vprop_sel #t p p_inj =
let varrayp_sel_depends_only_on (#t: Type) (p: (t -> vprop)) (p_inj: interp_hp_of_injective p) (m0: Steel.Memory.hmem (mk_selector_vprop_hp p)) (m1: mem{disjoint m0 m1}) : Lemma (mk_selector_vprop_sel' p p_inj m0 == mk_selector_vprop_sel' p p_inj (Steel.Memory.join m0 m1) ) [SMTPat (mk_selector_vprop_sel' p p_inj (Steel.Memory.join m0 m1))] = p_inj (mk_selector_vprop_sel' p p_inj m0) (mk_selector_vprop_sel' p p_inj (Steel.Memory.join m0 m1)) (Steel.Memory.join m0 m1) in let varrayp_sel_depends_only_on_core (#t: Type) (p: (t -> vprop)) (p_inj: interp_hp_of_injective p) (m0: Steel.Memory.hmem (mk_selector_vprop_hp p)) : Lemma (mk_selector_vprop_sel' p p_inj m0 == mk_selector_vprop_sel' p p_inj (core_mem m0)) [SMTPat (mk_selector_vprop_sel' p p_inj (core_mem m0))] = p_inj (mk_selector_vprop_sel' p p_inj m0) (mk_selector_vprop_sel' p p_inj (core_mem m0)) m0 in mk_selector_vprop_sel' p p_inj
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.cm_associativity
val cm_associativity (#c #eq: _) (cm: CE.cm c eq) : Lemma (forall (x: c) (y: c) (z: c). {:pattern ((x `cm.mult` y) `cm.mult` z)} ((x `cm.mult` y) `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z)))
val cm_associativity (#c #eq: _) (cm: CE.cm c eq) : Lemma (forall (x: c) (y: c) (z: c). {:pattern ((x `cm.mult` y) `cm.mult` z)} ((x `cm.mult` y) `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z)))
let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y z:c). {:pattern (x `cm.mult` y `cm.mult` z)} (x `cm.mult` y `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) = Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity)
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 73, "end_line": 483, "start_col": 0, "start_line": 480 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *) #push-options "--z3rlimit_factor 2 --fuel 1 --ifuel 0" let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0)) #pop-options //////////////////////////////////////////////////////////////////////////////// // foldm_snoc_split //////////////////////////////////////////////////////////////////////////////// (* Some utilities to introduce associativity-commutativity reasoning on CM using quantified formulas with patterns. Use these with care, since with large terms the SMT solver may end up with an explosion of instantiations
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cm: FStar.Algebra.CommMonoid.Equiv.cm c eq -> FStar.Pervasives.Lemma (ensures forall (x: c) (y: c) (z: c). {:pattern CM?.mult cm (CM?.mult cm x y) z} EQ?.eq eq (CM?.mult cm (CM?.mult cm x y) z) (CM?.mult cm x (CM?.mult cm y z)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Classical.forall_intro_3", "Prims.l_imp", "Prims.l_True", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "FStar.Classical.move_requires_3", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__associativity", "Prims.unit", "Prims.squash", "Prims.l_Forall", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x: c) (y: c) (z: c). {:pattern ((x `cm.mult` y) `cm.mult` z)} ((x `cm.mult` y) `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) =
Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity)
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.eq2_eq
val eq2_eq (#a: _) (eq: CE.equiv a) (x y: a) : Lemma (requires x == y) (ensures x `eq.eq` y)
val eq2_eq (#a: _) (eq: CE.equiv a) (x y: a) : Lemma (requires x == y) (ensures x `eq.eq` y)
let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 20, "end_line": 418, "start_col": 0, "start_line": 415 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
eq: FStar.Algebra.CommMonoid.Equiv.equiv a -> x: a -> y: a -> FStar.Pervasives.Lemma (requires x == y) (ensures EQ?.eq eq x y)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__reflexivity", "Prims.unit", "Prims.eq2", "Prims.squash", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let eq2_eq #a (eq: CE.equiv a) (x: a) (y: a) : Lemma (requires x == y) (ensures x `eq.eq` y) =
eq.reflexivity x
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.shift_perm
val shift_perm: #a: _ -> s0: seq a -> s1: seq a -> squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0) -> p: seqperm s0 s1 -> Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n)
val shift_perm: #a: _ -> s0: seq a -> s1: seq a -> squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0) -> p: seqperm s0 s1 -> Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n)
let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 26, "end_line": 407, "start_col": 0, "start_line": 396 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s0: FStar.Seq.Base.seq a -> s1: FStar.Seq.Base.seq a -> _: Prims.squash (FStar.Seq.Base.length s0 == FStar.Seq.Base.length s1 /\ FStar.Seq.Base.length s0 > 0) -> p: FStar.Seq.Permutation.seqperm s0 s1 -> Prims.Pure (FStar.Seq.Permutation.seqperm (FStar.Pervasives.Native.fst (FStar.Seq.Properties.un_snoc s0)) (FStar.Pervasives.Native.snd (FStar.Seq.Permutation.remove_i s1 (p (FStar.Seq.Base.length s0 - 1)))))
Prims.Pure
[]
[]
[ "FStar.Seq.Base.seq", "Prims.squash", "Prims.l_and", "Prims.eq2", "Prims.nat", "FStar.Seq.Base.length", "Prims.b2t", "Prims.op_GreaterThan", "FStar.Seq.Permutation.seqperm", "FStar.Seq.Permutation.shift_perm'", "Prims.unit", "FStar.Seq.Permutation.reveal_is_permutation", "FStar.Pervasives.Native.fst", "FStar.Seq.Properties.un_snoc", "FStar.Pervasives.Native.snd", "FStar.Seq.Permutation.remove_i", "Prims.op_Subtraction", "Prims.l_True", "FStar.Seq.Base.index", "Prims.int" ]
[]
false
false
false
false
false
let shift_perm #a (s0: seq a) (s1: seq a) (_: squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p: seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) =
reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc3
val foldm_snoc3 (#a #eq: _) (m: CE.cm a eq) (s1: seq a) (x: a) (s2: seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2))))
val foldm_snoc3 (#a #eq: _) (m: CE.cm a eq) (s1: seq a) (x: a) (s2: seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2))))
let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); }
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 368, "start_col": 0, "start_line": 334 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "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": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: FStar.Algebra.CommMonoid.Equiv.cm a eq -> s1: FStar.Seq.Base.seq a -> x: a -> s2: FStar.Seq.Base.seq a -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc m (FStar.Seq.Base.append s1 (FStar.Seq.Base.cons x s2))) (CM?.mult m x (FStar.Seq.Permutation.foldm_snoc m (FStar.Seq.Base.append s1 s2))))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Seq.Base.seq", "FStar.Calc.calc_finish", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Seq.Permutation.foldm_snoc", "FStar.Seq.Base.append", "FStar.Seq.Base.cons", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "FStar.Seq.Base.create", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Seq.Permutation.foldm_snoc_append", "Prims.squash", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__congruence", "FStar.Seq.Permutation.foldm_snoc_singleton", "FStar.Seq.Permutation.x_yz_to_y_xz", "FStar.Seq.Permutation.elim_monoid_laws", "FStar.Algebra.CommMonoid.Equiv.elim_eq_laws", "Prims.l_True", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let foldm_snoc3 #a #eq (m: CE.cm a eq) (s1: seq a) (x: a) (s2: seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) =
CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { (foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2))) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { (foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2))) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { (foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2))) } m.mult x (foldm_snoc m (Seq.append s1 s2)); }
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.cm_commutativity
val cm_commutativity (#c #eq: _) (cm: CE.cm c eq) : Lemma (forall (x: c) (y: c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x))
val cm_commutativity (#c #eq: _) (cm: CE.cm c eq) : Lemma (forall (x: c) (y: c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x))
let cm_commutativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y:c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x)) = Classical.forall_intro_2 (Classical.move_requires_2 cm.commutativity)
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 73, "end_line": 488, "start_col": 0, "start_line": 485 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *) #push-options "--z3rlimit_factor 2 --fuel 1 --ifuel 0" let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0)) #pop-options //////////////////////////////////////////////////////////////////////////////// // foldm_snoc_split //////////////////////////////////////////////////////////////////////////////// (* Some utilities to introduce associativity-commutativity reasoning on CM using quantified formulas with patterns. Use these with care, since with large terms the SMT solver may end up with an explosion of instantiations *) let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y z:c). {:pattern (x `cm.mult` y `cm.mult` z)} (x `cm.mult` y `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) = Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cm: FStar.Algebra.CommMonoid.Equiv.cm c eq -> FStar.Pervasives.Lemma (ensures forall (x: c) (y: c). {:pattern CM?.mult cm x y} EQ?.eq eq (CM?.mult cm x y) (CM?.mult cm y x) )
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Classical.forall_intro_2", "Prims.l_imp", "Prims.l_True", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "FStar.Classical.move_requires_2", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__commutativity", "Prims.unit", "Prims.squash", "Prims.l_Forall", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let cm_commutativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x: c) (y: c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x)) =
Classical.forall_intro_2 (Classical.move_requires_2 cm.commutativity)
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.seqperm_len
val seqperm_len (#a: _) (s0 s1: seq a) (p: seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1)
val seqperm_len (#a: _) (s0 s1: seq a) (p: seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1)
let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 33, "end_line": 413, "start_col": 0, "start_line": 409 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s0: FStar.Seq.Base.seq a -> s1: FStar.Seq.Base.seq a -> p: FStar.Seq.Permutation.seqperm s0 s1 -> FStar.Pervasives.Lemma (ensures FStar.Seq.Base.length s0 == FStar.Seq.Base.length s1)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Seq.Base.seq", "FStar.Seq.Permutation.seqperm", "FStar.Seq.Permutation.reveal_is_permutation", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "Prims.nat", "FStar.Seq.Base.length", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let seqperm_len #a (s0: seq a) (s1: seq a) (p: seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) =
reveal_is_permutation s0 s1 p
false
Pulse.PP.fst
Pulse.PP.printable_option
[@@ FStar.Tactics.Typeclasses.tcinstance] val printable_option: a: Type -> printable a -> printable (option a)
[@@ FStar.Tactics.Typeclasses.tcinstance] val printable_option: a: Type -> printable a -> printable (option a)
instance printable_option (a:Type) (_ : printable a) : printable (option a) = { pp = (function None -> doc_of_string "None" | Some v -> doc_of_string "Some" ^/^ pp v); }
{ "file_name": "lib/steel/pulse/Pulse.PP.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 1, "end_line": 67, "start_col": 0, "start_line": 64 }
(* Copyright 2023 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 Pulse.PP include FStar.Stubs.Pprint open FStar.Tactics open FStar.Tactics.Typeclasses open FStar.Stubs.Pprint open Pulse.Typing open Pulse.Syntax.Base open Pulse.Syntax.Printer open Pulse.Show (* A helper to create wrapped text *) val text : string -> FStar.Stubs.Pprint.document let text (s:string) : FStar.Stubs.Pprint.document = flow (break_ 1) (words s) (* Nests a document 2 levels deep, as a block. It inserts a hardline before the doc, so if you want to format something as hdr subdoc tail you should write hdr ^^ indent (subdoc) ^/^ tail. Note the ^^ vs ^/^. *) val indent : document -> document let indent d = nest 2 (hardline ^^ align d) class printable (a:Type) = { pp : a -> Tac document; } (* Repurposing a show instance *) let from_show #a {| d : tac_showable a |} : printable a = { pp = (fun x -> arbitrary_string (show x)); } instance _ : printable string = from_show instance _ : printable unit = from_show instance _ : printable bool = from_show instance _ : printable int = from_show instance _ : printable ctag = from_show
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.Env.fsti.checked", "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.Base.fsti.checked", "Pulse.Show.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Typeclasses.fsti.checked", "FStar.Tactics.fst.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": false, "source_file": "Pulse.PP.fst" }
[ { "abbrev": false, "full_module": "Pulse.Show", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax.Printer", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "FStar.Stubs.Pprint", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.Typeclasses", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Stubs.Pprint", "short_module": null }, { "abbrev": false, "full_module": "Pulse", "short_module": null }, { "abbrev": false, "full_module": "Pulse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> _: Pulse.PP.printable a -> Pulse.PP.printable (FStar.Pervasives.Native.option a)
Prims.Tot
[ "total" ]
[]
[ "Pulse.PP.printable", "Pulse.PP.Mkprintable", "FStar.Pervasives.Native.option", "FStar.Stubs.Pprint.doc_of_string", "FStar.Stubs.Pprint.document", "FStar.Stubs.Pprint.op_Hat_Slash_Hat", "Pulse.PP.pp" ]
[]
false
false
false
true
false
[@@ FStar.Tactics.Typeclasses.tcinstance] let printable_option (a: Type) (_: printable a) : printable (option a) =
{ pp = (function | None -> doc_of_string "None" | Some v -> doc_of_string "Some" ^/^ pp v) }
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.aux_shuffle_lemma
val aux_shuffle_lemma (#c #eq: _) (cm: CE.cm c eq) (s1 s2 l1 l2: c) : Lemma (((s1 `cm.mult` s2) `cm.mult` (l1 `cm.mult` l2)) `eq.eq` ((s1 `cm.mult` l1) `cm.mult` (s2 `cm.mult` l2)))
val aux_shuffle_lemma (#c #eq: _) (cm: CE.cm c eq) (s1 s2 l1 l2: c) : Lemma (((s1 `cm.mult` s2) `cm.mult` (l1 `cm.mult` l2)) `eq.eq` ((s1 `cm.mult` l1) `cm.mult` (s2 `cm.mult` l2)))
let aux_shuffle_lemma #c #eq (cm: CE.cm c eq) (s1 s2 l1 l2: c) : Lemma (((s1 `cm.mult` s2) `cm.mult` (l1 `cm.mult` l2)) `eq.eq` ((s1 `cm.mult` l1) `cm.mult` (s2 `cm.mult` l2))) = elim_eq_laws eq; cm_commutativity cm; cm_associativity cm; let (+) = cm.mult in cm.congruence (s1+s2) l1 (s2+s1) l1; cm.congruence ((s1+s2)+l1) l2 ((s2+s1)+l1) l2; cm.congruence ((s2+s1)+l1) l2 (s2+(s1+l1)) l2; cm.congruence (s2+(s1+l1)) l2 ((s1+l1)+s2) l2
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 49, "end_line": 535, "start_col": 0, "start_line": 524 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *) #push-options "--z3rlimit_factor 2 --fuel 1 --ifuel 0" let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0)) #pop-options //////////////////////////////////////////////////////////////////////////////// // foldm_snoc_split //////////////////////////////////////////////////////////////////////////////// (* Some utilities to introduce associativity-commutativity reasoning on CM using quantified formulas with patterns. Use these with care, since with large terms the SMT solver may end up with an explosion of instantiations *) let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y z:c). {:pattern (x `cm.mult` y `cm.mult` z)} (x `cm.mult` y `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) = Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity) let cm_commutativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y:c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x)) = Classical.forall_intro_2 (Classical.move_requires_2 cm.commutativity) (* A utility to introduce the equivalence relation laws into the context. FStar.Algebra.CommutativeMonoid provides something similar, but this version provides a more goal-directed pattern for transitivity. We should consider changing FStar.Algebra.CommutativeMonoid *) let elim_eq_laws #a (eq:CE.equiv a) : Lemma ( (forall x.{:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y.{:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z.{:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z) ) = CE.elim_eq_laws eq let fold_decomposition_aux #c #eq (cm: CE.cm c eq) (n0: int) (nk: int{nk=n0}) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Lemma (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))) = elim_eq_laws eq; let sum_of_funcs (i: under (closed_interval_size n0 nk)) = expr1 (n0+i) `cm.mult` expr2 (n0+i) in lemma_eq_elim (init (closed_interval_size n0 nk) sum_of_funcs) (create 1 (expr1 n0 `cm.mult` expr2 n0)); foldm_snoc_singleton cm (expr1 n0 `cm.mult` expr2 n0); let ts = (init (closed_interval_size n0 nk) sum_of_funcs) in let ts1 = (init (nk+1-n0) (fun i -> expr1 (n0+i))) in let ts2 = (init (nk+1-n0) (fun i -> expr2 (n0+i))) in assert (foldm_snoc cm ts `eq.eq` sum_of_funcs (nk-n0)); // this assert speeds up the proof. foldm_snoc_singleton cm (expr1 nk); foldm_snoc_singleton cm (expr2 nk); cm.congruence (foldm_snoc cm ts1) (foldm_snoc cm ts2) (expr1 nk) (expr2 nk)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cm: FStar.Algebra.CommMonoid.Equiv.cm c eq -> s1: c -> s2: c -> l1: c -> l2: c -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (CM?.mult cm (CM?.mult cm s1 s2) (CM?.mult cm l1 l2)) (CM?.mult cm (CM?.mult cm s1 l1) (CM?.mult cm s2 l2)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__congruence", "Prims.unit", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "FStar.Seq.Permutation.cm_associativity", "FStar.Seq.Permutation.cm_commutativity", "FStar.Seq.Permutation.elim_eq_laws", "Prims.l_True", "Prims.squash", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let aux_shuffle_lemma #c #eq (cm: CE.cm c eq) (s1: c) (s2: c) (l1: c) (l2: c) : Lemma (((s1 `cm.mult` s2) `cm.mult` (l1 `cm.mult` l2)) `eq.eq` ((s1 `cm.mult` l1) `cm.mult` (s2 `cm.mult` l2))) =
elim_eq_laws eq; cm_commutativity cm; cm_associativity cm; let ( + ) = cm.mult in cm.congruence (s1 + s2) l1 (s2 + s1) l1; cm.congruence ((s1 + s2) + l1) l2 ((s2 + s1) + l1) l2; cm.congruence ((s2 + s1) + l1) l2 (s2 + (s1 + l1)) l2; cm.congruence (s2 + (s1 + l1)) l2 ((s1 + l1) + s2) l2
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.shift_perm'
val shift_perm': #a: _ -> s0: seq a -> s1: seq a -> squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0) -> p: seqperm s0 s1 -> Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1)))))
val shift_perm': #a: _ -> s0: seq a -> s1: seq a -> squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0) -> p: seqperm s0 s1 -> Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1)))))
let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p'
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 6, "end_line": 393, "start_col": 0, "start_line": 378 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s0: FStar.Seq.Base.seq a -> s1: FStar.Seq.Base.seq a -> _: Prims.squash (FStar.Seq.Base.length s0 == FStar.Seq.Base.length s1 /\ FStar.Seq.Base.length s0 > 0) -> p: FStar.Seq.Permutation.seqperm s0 s1 -> FStar.Seq.Permutation.seqperm (FStar.Pervasives.Native.fst (FStar.Seq.Properties.un_snoc s0)) (FStar.Pervasives.Native.snd (FStar.Seq.Permutation.remove_i s1 (p (FStar.Seq.Base.length s0 - 1))))
Prims.Tot
[ "total" ]
[]
[ "FStar.Seq.Base.seq", "Prims.squash", "Prims.l_and", "Prims.eq2", "Prims.nat", "FStar.Seq.Base.length", "Prims.b2t", "Prims.op_GreaterThan", "FStar.Seq.Permutation.seqperm", "Prims.unit", "FStar.Seq.Permutation.reveal_is_permutation_nopats", "FStar.Pervasives.Native.fst", "FStar.Seq.Properties.un_snoc", "FStar.Pervasives.Native.snd", "FStar.Seq.Permutation.remove_i", "Prims.op_Subtraction", "FStar.Pervasives.Native.tuple2", "Prims.op_LessThan", "Prims.bool", "FStar.Seq.Properties.snoc", "FStar.Seq.Permutation.reveal_is_permutation" ]
[]
false
false
false
false
false
let shift_perm' #a (s0: seq a) (s1: seq a) (_: squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p: seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) =
reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i: nat{i < n}) : j: nat{j < n} = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p'
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc_split
val foldm_snoc_split (#c:_) (#eq:_) (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Lemma (ensures (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))))
val foldm_snoc_split (#c:_) (#eq:_) (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Lemma (ensures (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))))
let foldm_snoc_split #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) = foldm_snoc_split' cm n0 nk expr1 expr2
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 42, "end_line": 636, "start_col": 0, "start_line": 632 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *) #push-options "--z3rlimit_factor 2 --fuel 1 --ifuel 0" let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0)) #pop-options //////////////////////////////////////////////////////////////////////////////// // foldm_snoc_split //////////////////////////////////////////////////////////////////////////////// (* Some utilities to introduce associativity-commutativity reasoning on CM using quantified formulas with patterns. Use these with care, since with large terms the SMT solver may end up with an explosion of instantiations *) let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y z:c). {:pattern (x `cm.mult` y `cm.mult` z)} (x `cm.mult` y `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) = Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity) let cm_commutativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y:c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x)) = Classical.forall_intro_2 (Classical.move_requires_2 cm.commutativity) (* A utility to introduce the equivalence relation laws into the context. FStar.Algebra.CommutativeMonoid provides something similar, but this version provides a more goal-directed pattern for transitivity. We should consider changing FStar.Algebra.CommutativeMonoid *) let elim_eq_laws #a (eq:CE.equiv a) : Lemma ( (forall x.{:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y.{:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z.{:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z) ) = CE.elim_eq_laws eq let fold_decomposition_aux #c #eq (cm: CE.cm c eq) (n0: int) (nk: int{nk=n0}) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Lemma (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))) = elim_eq_laws eq; let sum_of_funcs (i: under (closed_interval_size n0 nk)) = expr1 (n0+i) `cm.mult` expr2 (n0+i) in lemma_eq_elim (init (closed_interval_size n0 nk) sum_of_funcs) (create 1 (expr1 n0 `cm.mult` expr2 n0)); foldm_snoc_singleton cm (expr1 n0 `cm.mult` expr2 n0); let ts = (init (closed_interval_size n0 nk) sum_of_funcs) in let ts1 = (init (nk+1-n0) (fun i -> expr1 (n0+i))) in let ts2 = (init (nk+1-n0) (fun i -> expr2 (n0+i))) in assert (foldm_snoc cm ts `eq.eq` sum_of_funcs (nk-n0)); // this assert speeds up the proof. foldm_snoc_singleton cm (expr1 nk); foldm_snoc_singleton cm (expr2 nk); cm.congruence (foldm_snoc cm ts1) (foldm_snoc cm ts2) (expr1 nk) (expr2 nk) let aux_shuffle_lemma #c #eq (cm: CE.cm c eq) (s1 s2 l1 l2: c) : Lemma (((s1 `cm.mult` s2) `cm.mult` (l1 `cm.mult` l2)) `eq.eq` ((s1 `cm.mult` l1) `cm.mult` (s2 `cm.mult` l2))) = elim_eq_laws eq; cm_commutativity cm; cm_associativity cm; let (+) = cm.mult in cm.congruence (s1+s2) l1 (s2+s1) l1; cm.congruence ((s1+s2)+l1) l2 ((s2+s1)+l1) l2; cm.congruence ((s2+s1)+l1) l2 (s2+(s1+l1)) l2; cm.congruence (s2+(s1+l1)) l2 ((s1+l1)+s2) l2 #push-options "--ifuel 0 --fuel 1 --z3rlimit 40" (* This proof is quite delicate, for several reasons: - It's working with higher order functions that are non-trivially dependently typed, notably on the ranges the ranges of indexes they manipulate - When using the induction hypothesis (i.e., on a recursive call), what we get is a property about the function at a different type, i.e., `range_count n0 (nk - 1)`. - If left to the SMT solver alone, these higher order functions at slightly different types cannot be proven equal and the proof fails, often mysteriously. - To have something more robust, I rewrote this function to return a squash proof, and then to coerce this proof to the type needed, where the F* unififer/normalization machinery can help, rather than leaving it purely to SMT, which is what happens when the property is states as a postcondition of a Lemma *) let rec foldm_snoc_split' #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Tot (squash (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk))))) (decreases nk-n0) = if (nk=n0) then fold_decomposition_aux cm n0 nk expr1 expr2 else ( cm_commutativity cm; elim_eq_laws eq; let lfunc_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr (func_sum cm expr1 expr2) n0 nf in let range_count = closed_interval_size in let full_count = range_count n0 nk in let sub_count = range_count n0 (nk-1) in let fullseq = init full_count (lfunc_up_to nk) in let rfunc_1_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr1 n0 nf in let rfunc_2_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr2 n0 nf in let fullseq_r1 = init full_count (rfunc_1_up_to nk) in let fullseq_r2 = init full_count (rfunc_2_up_to nk) in let subseq = init sub_count (lfunc_up_to nk) in let subfold = foldm_snoc cm subseq in let last = lfunc_up_to nk sub_count in lemma_eq_elim (fst (un_snoc fullseq)) subseq; // subseq is literally (liat fullseq) let fullfold = foldm_snoc cm fullseq in let subseq_r1 = init sub_count (rfunc_1_up_to nk) in let subseq_r2 = init sub_count (rfunc_2_up_to nk) in lemma_eq_elim (fst (un_snoc fullseq_r1)) subseq_r1; // subseq is literally (liat fullseq) lemma_eq_elim (fst (un_snoc fullseq_r2)) subseq_r2; // subseq is literally (liat fullseq) lemma_eq_elim (init sub_count (lfunc_up_to nk)) subseq; lemma_eq_elim (init sub_count (lfunc_up_to (nk-1))) subseq; lemma_eq_elim subseq_r1 (init sub_count (rfunc_1_up_to (nk-1))); lemma_eq_elim subseq_r2 (init sub_count (rfunc_2_up_to (nk-1))); let fullfold_r1 = foldm_snoc cm fullseq_r1 in let fullfold_r2 = foldm_snoc cm fullseq_r2 in let subfold_r1 = foldm_snoc cm subseq_r1 in let subfold_r2 = foldm_snoc cm subseq_r2 in cm.congruence (foldm_snoc cm (init sub_count (rfunc_1_up_to (nk-1)))) (foldm_snoc cm (init sub_count (rfunc_2_up_to (nk-1)))) subfold_r1 subfold_r2; let last_r1 = rfunc_1_up_to nk sub_count in let last_r2 = rfunc_2_up_to nk sub_count in let nk' = nk - 1 in (* here's the nasty bit with where we have to massage the proof from the induction hypothesis *) let ih : squash ((foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')) `eq.eq` cm.mult (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))) (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))))) = foldm_snoc_split' cm n0 nk' expr1 expr2 in calc (==) { subfold; == { } foldm_snoc cm subseq; == { assert (Seq.equal subseq (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk'))) } foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')); }; assert (Seq.equal subseq_r1 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))); assert (Seq.equal subseq_r2 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))); let _ : squash (subfold `eq.eq` (subfold_r1 `cm.mult` subfold_r2)) = ih in cm.congruence subfold last (subfold_r1 `cm.mult` subfold_r2) last; aux_shuffle_lemma cm subfold_r1 subfold_r2 (rfunc_1_up_to nk sub_count) (rfunc_2_up_to nk sub_count); cm.congruence (subfold_r1 `cm.mult` (rfunc_1_up_to nk sub_count)) (subfold_r2 `cm.mult` (rfunc_2_up_to nk sub_count)) (foldm_snoc cm fullseq_r1) (foldm_snoc cm fullseq_r2) ) #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cm: FStar.Algebra.CommMonoid.Equiv.cm c eq -> n0: Prims.int -> nk: FStar.IntegerIntervals.not_less_than n0 -> expr1: (_: FStar.IntegerIntervals.ifrom_ito n0 nk -> c) -> expr2: (_: FStar.IntegerIntervals.ifrom_ito n0 nk -> c) -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init (FStar.IntegerIntervals.closed_interval_size n0 nk) (FStar.Seq.Permutation.init_func_from_expr (FStar.Seq.Permutation.func_sum cm expr1 expr2) n0 nk))) (CM?.mult cm (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init (FStar.IntegerIntervals.closed_interval_size n0 nk) (FStar.Seq.Permutation.init_func_from_expr expr1 n0 nk))) (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init (FStar.IntegerIntervals.closed_interval_size n0 nk) (FStar.Seq.Permutation.init_func_from_expr expr2 n0 nk)))))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "Prims.int", "FStar.IntegerIntervals.not_less_than", "FStar.IntegerIntervals.ifrom_ito", "FStar.Seq.Permutation.foldm_snoc_split'", "Prims.unit" ]
[]
true
false
true
false
false
let foldm_snoc_split #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1: ((ifrom_ito n0 nk) -> c)) (expr2: ((ifrom_ito n0 nk) -> c)) =
foldm_snoc_split' cm n0 nk expr1 expr2
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc_append
val foldm_snoc_append (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2)))
val foldm_snoc_append (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2)))
let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); })
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 8, "end_line": 322, "start_col": 0, "start_line": 294 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 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": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 2, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: FStar.Algebra.CommMonoid.Equiv.cm a eq -> s1: FStar.Seq.Base.seq a -> s2: FStar.Seq.Base.seq a -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc m (FStar.Seq.Base.append s1 s2)) (CM?.mult m (FStar.Seq.Permutation.foldm_snoc m s1) (FStar.Seq.Permutation.foldm_snoc m s2))) (decreases FStar.Seq.Base.length s2)
FStar.Pervasives.Lemma
[ "", "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Seq.Base.seq", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Base.append", "Prims.bool", "FStar.Calc.calc_finish", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Seq.Permutation.foldm_snoc", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "FStar.Seq.Properties.snoc", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "Prims.squash", "FStar.Pervasives.Native.fst", "FStar.Seq.Properties.un_snoc", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__congruence", "FStar.Seq.Permutation.foldm_snoc_append", "FStar.Seq.Permutation.x_yz_to_y_xz", "FStar.Pervasives.Native.tuple2", "Prims.eq2", "FStar.Pervasives.Native.snd", "FStar.Seq.Permutation.elim_monoid_laws", "FStar.Algebra.CommMonoid.Equiv.elim_eq_laws", "Prims.l_True", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec foldm_snoc_append #a #eq (m: CE.cm a eq) (s1: seq a) (s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) =
CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert ((Seq.append s1 s2) `Seq.equal` s1) else (let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { (foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2'))) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { () } m.mult (foldm_snoc m s1) (foldm_snoc m s2); })
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.intro_exists
val intro_exists (#a:Type) (#opened_invariants:_) (x:a) (p:a -> vprop) : SteelGhostT unit opened_invariants (p x) (fun _ -> h_exists p)
val intro_exists (#a:Type) (#opened_invariants:_) (x:a) (p:a -> vprop) : SteelGhostT unit opened_invariants (p x) (fun _ -> h_exists p)
let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m)
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 95, "end_line": 611, "start_col": 0, "start_line": 610 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: a -> p: (_: a -> Steel.Effect.Common.vprop) -> Steel.Effect.Atomic.SteelGhostT Prims.unit
Steel.Effect.Atomic.SteelGhostT
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Atomic.rewrite_slprop", "Steel.Effect.Atomic.h_exists", "Steel.Memory.mem", "Steel.Memory.intro_h_exists", "Steel.Effect.Atomic.h_exists_sl'", "Prims.unit" ]
[]
false
true
false
false
false
let intro_exists #a #opened x p =
rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m)
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc_equality
val foldm_snoc_equality (#c:_) (#eq:_) (add: CE.cm c eq) (s t: seq c) : Lemma (requires length s == length t /\ eq_of_seq eq s t) (ensures foldm_snoc add s `eq.eq` foldm_snoc add t)
val foldm_snoc_equality (#c:_) (#eq:_) (add: CE.cm c eq) (s t: seq c) : Lemma (requires length s == length t /\ eq_of_seq eq s t) (ensures foldm_snoc add s `eq.eq` foldm_snoc add t)
let rec foldm_snoc_equality #c #eq (add: CE.cm c eq) (s t: seq c) : Lemma (requires length s == length t /\ eq_of_seq eq s t) (ensures foldm_snoc add s `eq.eq` foldm_snoc add t) (decreases length s) = if length s = 0 then ( assert_norm (foldm_snoc add s == add.unit); assert_norm (foldm_snoc add t == add.unit); eq.reflexivity add.unit ) else ( let sliat, slast = un_snoc s in let tliat, tlast = un_snoc t in eq_of_seq_unsnoc eq (length s) s t; foldm_snoc_equality add sliat tliat; add.congruence slast (foldm_snoc add sliat) tlast (foldm_snoc add tliat) )
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 656, "start_col": 0, "start_line": 640 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *) #push-options "--z3rlimit_factor 2 --fuel 1 --ifuel 0" let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0)) #pop-options //////////////////////////////////////////////////////////////////////////////// // foldm_snoc_split //////////////////////////////////////////////////////////////////////////////// (* Some utilities to introduce associativity-commutativity reasoning on CM using quantified formulas with patterns. Use these with care, since with large terms the SMT solver may end up with an explosion of instantiations *) let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y z:c). {:pattern (x `cm.mult` y `cm.mult` z)} (x `cm.mult` y `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) = Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity) let cm_commutativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y:c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x)) = Classical.forall_intro_2 (Classical.move_requires_2 cm.commutativity) (* A utility to introduce the equivalence relation laws into the context. FStar.Algebra.CommutativeMonoid provides something similar, but this version provides a more goal-directed pattern for transitivity. We should consider changing FStar.Algebra.CommutativeMonoid *) let elim_eq_laws #a (eq:CE.equiv a) : Lemma ( (forall x.{:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y.{:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z.{:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z) ) = CE.elim_eq_laws eq let fold_decomposition_aux #c #eq (cm: CE.cm c eq) (n0: int) (nk: int{nk=n0}) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Lemma (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))) = elim_eq_laws eq; let sum_of_funcs (i: under (closed_interval_size n0 nk)) = expr1 (n0+i) `cm.mult` expr2 (n0+i) in lemma_eq_elim (init (closed_interval_size n0 nk) sum_of_funcs) (create 1 (expr1 n0 `cm.mult` expr2 n0)); foldm_snoc_singleton cm (expr1 n0 `cm.mult` expr2 n0); let ts = (init (closed_interval_size n0 nk) sum_of_funcs) in let ts1 = (init (nk+1-n0) (fun i -> expr1 (n0+i))) in let ts2 = (init (nk+1-n0) (fun i -> expr2 (n0+i))) in assert (foldm_snoc cm ts `eq.eq` sum_of_funcs (nk-n0)); // this assert speeds up the proof. foldm_snoc_singleton cm (expr1 nk); foldm_snoc_singleton cm (expr2 nk); cm.congruence (foldm_snoc cm ts1) (foldm_snoc cm ts2) (expr1 nk) (expr2 nk) let aux_shuffle_lemma #c #eq (cm: CE.cm c eq) (s1 s2 l1 l2: c) : Lemma (((s1 `cm.mult` s2) `cm.mult` (l1 `cm.mult` l2)) `eq.eq` ((s1 `cm.mult` l1) `cm.mult` (s2 `cm.mult` l2))) = elim_eq_laws eq; cm_commutativity cm; cm_associativity cm; let (+) = cm.mult in cm.congruence (s1+s2) l1 (s2+s1) l1; cm.congruence ((s1+s2)+l1) l2 ((s2+s1)+l1) l2; cm.congruence ((s2+s1)+l1) l2 (s2+(s1+l1)) l2; cm.congruence (s2+(s1+l1)) l2 ((s1+l1)+s2) l2 #push-options "--ifuel 0 --fuel 1 --z3rlimit 40" (* This proof is quite delicate, for several reasons: - It's working with higher order functions that are non-trivially dependently typed, notably on the ranges the ranges of indexes they manipulate - When using the induction hypothesis (i.e., on a recursive call), what we get is a property about the function at a different type, i.e., `range_count n0 (nk - 1)`. - If left to the SMT solver alone, these higher order functions at slightly different types cannot be proven equal and the proof fails, often mysteriously. - To have something more robust, I rewrote this function to return a squash proof, and then to coerce this proof to the type needed, where the F* unififer/normalization machinery can help, rather than leaving it purely to SMT, which is what happens when the property is states as a postcondition of a Lemma *) let rec foldm_snoc_split' #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Tot (squash (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk))))) (decreases nk-n0) = if (nk=n0) then fold_decomposition_aux cm n0 nk expr1 expr2 else ( cm_commutativity cm; elim_eq_laws eq; let lfunc_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr (func_sum cm expr1 expr2) n0 nf in let range_count = closed_interval_size in let full_count = range_count n0 nk in let sub_count = range_count n0 (nk-1) in let fullseq = init full_count (lfunc_up_to nk) in let rfunc_1_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr1 n0 nf in let rfunc_2_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr2 n0 nf in let fullseq_r1 = init full_count (rfunc_1_up_to nk) in let fullseq_r2 = init full_count (rfunc_2_up_to nk) in let subseq = init sub_count (lfunc_up_to nk) in let subfold = foldm_snoc cm subseq in let last = lfunc_up_to nk sub_count in lemma_eq_elim (fst (un_snoc fullseq)) subseq; // subseq is literally (liat fullseq) let fullfold = foldm_snoc cm fullseq in let subseq_r1 = init sub_count (rfunc_1_up_to nk) in let subseq_r2 = init sub_count (rfunc_2_up_to nk) in lemma_eq_elim (fst (un_snoc fullseq_r1)) subseq_r1; // subseq is literally (liat fullseq) lemma_eq_elim (fst (un_snoc fullseq_r2)) subseq_r2; // subseq is literally (liat fullseq) lemma_eq_elim (init sub_count (lfunc_up_to nk)) subseq; lemma_eq_elim (init sub_count (lfunc_up_to (nk-1))) subseq; lemma_eq_elim subseq_r1 (init sub_count (rfunc_1_up_to (nk-1))); lemma_eq_elim subseq_r2 (init sub_count (rfunc_2_up_to (nk-1))); let fullfold_r1 = foldm_snoc cm fullseq_r1 in let fullfold_r2 = foldm_snoc cm fullseq_r2 in let subfold_r1 = foldm_snoc cm subseq_r1 in let subfold_r2 = foldm_snoc cm subseq_r2 in cm.congruence (foldm_snoc cm (init sub_count (rfunc_1_up_to (nk-1)))) (foldm_snoc cm (init sub_count (rfunc_2_up_to (nk-1)))) subfold_r1 subfold_r2; let last_r1 = rfunc_1_up_to nk sub_count in let last_r2 = rfunc_2_up_to nk sub_count in let nk' = nk - 1 in (* here's the nasty bit with where we have to massage the proof from the induction hypothesis *) let ih : squash ((foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')) `eq.eq` cm.mult (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))) (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))))) = foldm_snoc_split' cm n0 nk' expr1 expr2 in calc (==) { subfold; == { } foldm_snoc cm subseq; == { assert (Seq.equal subseq (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk'))) } foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')); }; assert (Seq.equal subseq_r1 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))); assert (Seq.equal subseq_r2 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))); let _ : squash (subfold `eq.eq` (subfold_r1 `cm.mult` subfold_r2)) = ih in cm.congruence subfold last (subfold_r1 `cm.mult` subfold_r2) last; aux_shuffle_lemma cm subfold_r1 subfold_r2 (rfunc_1_up_to nk sub_count) (rfunc_2_up_to nk sub_count); cm.congruence (subfold_r1 `cm.mult` (rfunc_1_up_to nk sub_count)) (subfold_r2 `cm.mult` (rfunc_2_up_to nk sub_count)) (foldm_snoc cm fullseq_r1) (foldm_snoc cm fullseq_r2) ) #pop-options /// Finally, package the proof up into a Lemma, as expected by the interface let foldm_snoc_split #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) = foldm_snoc_split' cm n0 nk expr1 expr2 open FStar.Seq.Equiv
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
add: FStar.Algebra.CommMonoid.Equiv.cm c eq -> s: FStar.Seq.Base.seq c -> t: FStar.Seq.Base.seq c -> FStar.Pervasives.Lemma (requires FStar.Seq.Base.length s == FStar.Seq.Base.length t /\ FStar.Seq.Equiv.eq_of_seq eq s t) (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc add s) (FStar.Seq.Permutation.foldm_snoc add t)) (decreases FStar.Seq.Base.length s)
FStar.Pervasives.Lemma
[ "", "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Seq.Base.seq", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__reflexivity", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit", "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.eq2", "FStar.Seq.Permutation.foldm_snoc", "Prims.bool", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__congruence", "FStar.Seq.Permutation.foldm_snoc_equality", "FStar.Seq.Equiv.eq_of_seq_unsnoc", "FStar.Pervasives.Native.tuple2", "FStar.Seq.Properties.snoc", "FStar.Pervasives.Native.fst", "FStar.Pervasives.Native.snd", "FStar.Seq.Properties.un_snoc", "Prims.l_and", "Prims.nat", "FStar.Seq.Equiv.eq_of_seq", "Prims.squash", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec foldm_snoc_equality #c #eq (add: CE.cm c eq) (s: seq c) (t: seq c) : Lemma (requires length s == length t /\ eq_of_seq eq s t) (ensures (foldm_snoc add s) `eq.eq` (foldm_snoc add t)) (decreases length s) =
if length s = 0 then (assert_norm (foldm_snoc add s == add.unit); assert_norm (foldm_snoc add t == add.unit); eq.reflexivity add.unit) else (let sliat, slast = un_snoc s in let tliat, tlast = un_snoc t in eq_of_seq_unsnoc eq (length s) s t; foldm_snoc_equality add sliat tliat; add.congruence slast (foldm_snoc add sliat) tlast (foldm_snoc add tliat))
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.as_atomic_o_action
val as_atomic_o_action (#a: Type u#a) (#opened_invariants: inames) (#fp: slprop) (#fp': (a -> slprop)) (o: observability) (f: action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x))
val as_atomic_o_action (#a: Type u#a) (#opened_invariants: inames) (#fp: slprop) (#fp': (a -> slprop)) (o: observability) (f: action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x))
let as_atomic_o_action (#a:Type u#a) (#opened_invariants:inames) (#fp:slprop) (#fp': a -> slprop) (o:observability) (f:action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) = SteelAtomicBaseT?.reflect f
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 31, "end_line": 663, "start_col": 0, "start_line": 655 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i (* * AR: SteelAtomic and SteelGhost are not marked reifiable since we intend to run Steel programs natively * However to implement the with_inv combinators we need to reify their thunks to reprs * We could implement it better by having support for reification only in the .fst file * But for now assuming a function *) assume val reify_steel_atomic_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#g:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelAtomicBase a framed opened_invariants g pre post req ens) : repr a framed opened_invariants g pre post req ens
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
o: Steel.Effect.Common.observability -> f: Steel.Memory.action_except a opened_invariants fp fp' -> Steel.Effect.Atomic.SteelAtomicBaseT a
Steel.Effect.Atomic.SteelAtomicBaseT
[]
[]
[ "Steel.Memory.inames", "Steel.Memory.slprop", "Steel.Effect.Common.observability", "Steel.Memory.action_except", "Steel.Effect.Common.to_vprop", "Steel.Effect.Common.vprop" ]
[]
false
true
false
false
false
let as_atomic_o_action (#a: Type u#a) (#opened_invariants: inames) (#fp: slprop) (#fp': (a -> slprop)) (o: observability) (f: action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) =
SteelAtomicBaseT?.reflect f
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.fold_decomposition_aux
val fold_decomposition_aux (#c #eq: _) (cm: CE.cm c eq) (n0: int) (nk: int{nk = n0}) (expr1 expr2: ((ifrom_ito n0 nk) -> c)) : Lemma ((foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk))) `eq.eq` (cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))))
val fold_decomposition_aux (#c #eq: _) (cm: CE.cm c eq) (n0: int) (nk: int{nk = n0}) (expr1 expr2: ((ifrom_ito n0 nk) -> c)) : Lemma ((foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk))) `eq.eq` (cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))))
let fold_decomposition_aux #c #eq (cm: CE.cm c eq) (n0: int) (nk: int{nk=n0}) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Lemma (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))) = elim_eq_laws eq; let sum_of_funcs (i: under (closed_interval_size n0 nk)) = expr1 (n0+i) `cm.mult` expr2 (n0+i) in lemma_eq_elim (init (closed_interval_size n0 nk) sum_of_funcs) (create 1 (expr1 n0 `cm.mult` expr2 n0)); foldm_snoc_singleton cm (expr1 n0 `cm.mult` expr2 n0); let ts = (init (closed_interval_size n0 nk) sum_of_funcs) in let ts1 = (init (nk+1-n0) (fun i -> expr1 (n0+i))) in let ts2 = (init (nk+1-n0) (fun i -> expr2 (n0+i))) in assert (foldm_snoc cm ts `eq.eq` sum_of_funcs (nk-n0)); // this assert speeds up the proof. foldm_snoc_singleton cm (expr1 nk); foldm_snoc_singleton cm (expr2 nk); cm.congruence (foldm_snoc cm ts1) (foldm_snoc cm ts2) (expr1 nk) (expr2 nk)
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 79, "end_line": 522, "start_col": 0, "start_line": 502 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *) #push-options "--z3rlimit_factor 2 --fuel 1 --ifuel 0" let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0)) #pop-options //////////////////////////////////////////////////////////////////////////////// // foldm_snoc_split //////////////////////////////////////////////////////////////////////////////// (* Some utilities to introduce associativity-commutativity reasoning on CM using quantified formulas with patterns. Use these with care, since with large terms the SMT solver may end up with an explosion of instantiations *) let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y z:c). {:pattern (x `cm.mult` y `cm.mult` z)} (x `cm.mult` y `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) = Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity) let cm_commutativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y:c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x)) = Classical.forall_intro_2 (Classical.move_requires_2 cm.commutativity) (* A utility to introduce the equivalence relation laws into the context. FStar.Algebra.CommutativeMonoid provides something similar, but this version provides a more goal-directed pattern for transitivity. We should consider changing FStar.Algebra.CommutativeMonoid *) let elim_eq_laws #a (eq:CE.equiv a) : Lemma ( (forall x.{:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y.{:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z.{:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z) ) = CE.elim_eq_laws eq
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cm: FStar.Algebra.CommMonoid.Equiv.cm c eq -> n0: Prims.int -> nk: Prims.int{nk = n0} -> expr1: (_: FStar.IntegerIntervals.ifrom_ito n0 nk -> c) -> expr2: (_: FStar.IntegerIntervals.ifrom_ito n0 nk -> c) -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init (FStar.IntegerIntervals.closed_interval_size n0 nk) (FStar.Seq.Permutation.init_func_from_expr (FStar.Seq.Permutation.func_sum cm expr1 expr2) n0 nk))) (CM?.mult cm (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init (FStar.IntegerIntervals.closed_interval_size n0 nk) (FStar.Seq.Permutation.init_func_from_expr expr1 n0 nk))) (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init (FStar.IntegerIntervals.closed_interval_size n0 nk) (FStar.Seq.Permutation.init_func_from_expr expr2 n0 nk)))))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "Prims.int", "Prims.b2t", "Prims.op_Equality", "FStar.IntegerIntervals.ifrom_ito", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__congruence", "FStar.Seq.Permutation.foldm_snoc", "Prims.unit", "FStar.Seq.Permutation.foldm_snoc_singleton", "Prims._assert", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "Prims.op_Subtraction", "FStar.Seq.Base.seq", "FStar.Seq.Base.init", "Prims.op_Addition", "Prims.nat", "Prims.op_LessThan", "FStar.IntegerIntervals.closed_interval_size", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "FStar.Seq.Base.lemma_eq_elim", "FStar.Seq.Base.create", "FStar.IntegerIntervals.under", "FStar.Seq.Permutation.elim_eq_laws", "Prims.l_True", "Prims.squash", "FStar.Seq.Permutation.init_func_from_expr", "FStar.Seq.Permutation.func_sum", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let fold_decomposition_aux #c #eq (cm: CE.cm c eq) (n0: int) (nk: int{nk = n0}) (expr1: ((ifrom_ito n0 nk) -> c)) (expr2: ((ifrom_ito n0 nk) -> c)) : Lemma ((foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk))) `eq.eq` (cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk))))) =
elim_eq_laws eq; let sum_of_funcs (i: under (closed_interval_size n0 nk)) = (expr1 (n0 + i)) `cm.mult` (expr2 (n0 + i)) in lemma_eq_elim (init (closed_interval_size n0 nk) sum_of_funcs) (create 1 ((expr1 n0) `cm.mult` (expr2 n0))); foldm_snoc_singleton cm ((expr1 n0) `cm.mult` (expr2 n0)); let ts = (init (closed_interval_size n0 nk) sum_of_funcs) in let ts1 = (init (nk + 1 - n0) (fun i -> expr1 (n0 + i))) in let ts2 = (init (nk + 1 - n0) (fun i -> expr2 (n0 + i))) in assert ((foldm_snoc cm ts) `eq.eq` (sum_of_funcs (nk - n0))); foldm_snoc_singleton cm (expr1 nk); foldm_snoc_singleton cm (expr2 nk); cm.congruence (foldm_snoc cm ts1) (foldm_snoc cm ts2) (expr1 nk) (expr2 nk)
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.intro_exists_erased
val intro_exists_erased (#a:Type) (#opened_invariants:_) (x:Ghost.erased a) (p:a -> vprop) : SteelGhostT unit opened_invariants (p x) (fun _ -> h_exists p)
val intro_exists_erased (#a:Type) (#opened_invariants:_) (x:Ghost.erased a) (p:a -> vprop) : SteelGhostT unit opened_invariants (p x) (fun _ -> h_exists p)
let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m)
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 78, "end_line": 615, "start_col": 0, "start_line": 613 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.Ghost.erased a -> p: (_: a -> Steel.Effect.Common.vprop) -> Steel.Effect.Atomic.SteelGhostT Prims.unit
Steel.Effect.Atomic.SteelGhostT
[]
[]
[ "Steel.Memory.inames", "FStar.Ghost.erased", "Steel.Effect.Common.vprop", "Steel.Effect.Atomic.rewrite_slprop", "FStar.Ghost.reveal", "Steel.Effect.Atomic.h_exists", "Steel.Memory.mem", "Steel.Memory.intro_h_exists", "Steel.Effect.Atomic.h_exists_sl'", "Prims.unit" ]
[]
false
true
false
false
false
let intro_exists_erased #a #opened x p =
rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m)
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc_perm
val foldm_snoc_perm (#a:_) (#eq:_) (m:CE.cm a eq) (s0:seq a) (s1:seq a) (p:seqperm s0 s1) : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1))
val foldm_snoc_perm (#a:_) (#eq:_) (m:CE.cm a eq) (s0:seq a) (s1:seq a) (p:seqperm s0 s1) : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1))
let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0))
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 54, "end_line": 467, "start_col": 0, "start_line": 422 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 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": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 2, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
m: FStar.Algebra.CommMonoid.Equiv.cm a eq -> s0: FStar.Seq.Base.seq a -> s1: FStar.Seq.Base.seq a -> p: FStar.Seq.Permutation.seqperm s0 s1 -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc m s0) (FStar.Seq.Permutation.foldm_snoc m s1)) (decreases FStar.Seq.Base.length s0)
FStar.Pervasives.Lemma
[ "", "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Seq.Base.seq", "FStar.Seq.Permutation.seqperm", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "FStar.Seq.Permutation.eq2_eq", "FStar.Seq.Permutation.foldm_snoc", "Prims.unit", "Prims._assert", "FStar.Seq.Base.equal", "Prims.bool", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__symmetry", "FStar.Calc.calc_finish", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "FStar.Calc.calc_step", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "FStar.Seq.Base.append", "FStar.Seq.Base.cons", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "Prims.squash", "FStar.Seq.Permutation.foldm_snoc3", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__congruence", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__reflexivity", "FStar.Seq.Permutation.foldm_snoc_perm", "Prims.eq2", "FStar.Seq.Permutation.shift_perm", "FStar.Pervasives.Native.snd", "FStar.Seq.Permutation.remove_i", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "FStar.Seq.Properties.head", "FStar.Seq.Properties.tail", "FStar.Seq.Properties.split", "FStar.Seq.Properties.snoc", "FStar.Pervasives.Native.fst", "FStar.Seq.Properties.un_snoc", "Prims.op_Subtraction", "FStar.Seq.Permutation.seqperm_len", "FStar.Algebra.CommMonoid.Equiv.elim_eq_laws", "Prims.l_True", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) =
CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then (assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1)) else (let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p':seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { (assert (s1 `Seq.equal` (Seq.append prefix' (Seq.cons last' suffix'))); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { (assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1'))) } m.mult last' (foldm_snoc m s1'); (eq.eq) { (foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix)) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0))
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.exists_cong
val exists_cong (#a:_) (#u:_) (p:a -> vprop) (q:a -> vprop {forall x. equiv (p x) (q x) }) : SteelGhostT unit u (h_exists p) (fun _ -> h_exists q)
val exists_cong (#a:_) (#u:_) (p:a -> vprop) (q:a -> vprop {forall x. equiv (p x) (q x) }) : SteelGhostT unit u (h_exists p) (fun _ -> h_exists q)
let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q)
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 23, "end_line": 631, "start_col": 0, "start_line": 627 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: (_: a -> Steel.Effect.Common.vprop) -> q: (_: a -> Steel.Effect.Common.vprop){forall (x: a). Steel.Effect.Common.equiv (p x) (q x)} -> Steel.Effect.Atomic.SteelGhostT Prims.unit
Steel.Effect.Atomic.SteelGhostT
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Prims.l_Forall", "Steel.Effect.Common.equiv", "Steel.Effect.Atomic.rewrite_slprop", "Steel.Effect.Atomic.h_exists", "Steel.Memory.mem", "Steel.Effect.Atomic.exists_equiv", "Prims.unit", "Steel.Effect.Common.reveal_equiv" ]
[]
false
true
false
false
false
let exists_cong p q =
rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q)
false
Pulse.PP.fst
Pulse.PP.separate_map
val separate_map (sep: document) (f: ('a -> Tac document)) (l: list 'a) : Tac document
val separate_map (sep: document) (f: ('a -> Tac document)) (l: list 'a) : Tac document
let rec separate_map (sep: document) (f : 'a -> Tac document) (l : list 'a) : Tac document = match l with | [] -> empty | [x] -> f x | x::xs -> f x ^^ sep ^/^ separate_map sep f xs
{ "file_name": "lib/steel/pulse/Pulse.PP.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 49, "end_line": 75, "start_col": 0, "start_line": 71 }
(* Copyright 2023 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 Pulse.PP include FStar.Stubs.Pprint open FStar.Tactics open FStar.Tactics.Typeclasses open FStar.Stubs.Pprint open Pulse.Typing open Pulse.Syntax.Base open Pulse.Syntax.Printer open Pulse.Show (* A helper to create wrapped text *) val text : string -> FStar.Stubs.Pprint.document let text (s:string) : FStar.Stubs.Pprint.document = flow (break_ 1) (words s) (* Nests a document 2 levels deep, as a block. It inserts a hardline before the doc, so if you want to format something as hdr subdoc tail you should write hdr ^^ indent (subdoc) ^/^ tail. Note the ^^ vs ^/^. *) val indent : document -> document let indent d = nest 2 (hardline ^^ align d) class printable (a:Type) = { pp : a -> Tac document; } (* Repurposing a show instance *) let from_show #a {| d : tac_showable a |} : printable a = { pp = (fun x -> arbitrary_string (show x)); } instance _ : printable string = from_show instance _ : printable unit = from_show instance _ : printable bool = from_show instance _ : printable int = from_show instance _ : printable ctag = from_show instance printable_option (a:Type) (_ : printable a) : printable (option a) = { pp = (function None -> doc_of_string "None" | Some v -> doc_of_string "Some" ^/^ pp v); } // Cannot use Pprint.separate_map, it takes a pure func
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.Env.fsti.checked", "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.Base.fsti.checked", "Pulse.Show.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Typeclasses.fsti.checked", "FStar.Tactics.fst.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": false, "source_file": "Pulse.PP.fst" }
[ { "abbrev": false, "full_module": "Pulse.Show", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax.Printer", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "FStar.Stubs.Pprint", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.Typeclasses", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics", "short_module": null }, { "abbrev": false, "full_module": "FStar.Stubs.Pprint", "short_module": null }, { "abbrev": false, "full_module": "Pulse", "short_module": null }, { "abbrev": false, "full_module": "Pulse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
sep: FStar.Stubs.Pprint.document -> f: (_: 'a -> FStar.Tactics.Effect.Tac FStar.Stubs.Pprint.document) -> l: Prims.list 'a -> FStar.Tactics.Effect.Tac FStar.Stubs.Pprint.document
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Stubs.Pprint.document", "Prims.list", "FStar.Stubs.Pprint.empty", "FStar.Stubs.Pprint.op_Hat_Hat", "FStar.Stubs.Pprint.op_Hat_Slash_Hat", "Pulse.PP.separate_map" ]
[ "recursion" ]
false
true
false
false
false
let rec separate_map (sep: document) (f: ('a -> Tac document)) (l: list 'a) : Tac document =
match l with | [] -> empty | [x] -> f x | x :: xs -> f x ^^ sep ^/^ separate_map sep f xs
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc_split_seq
val foldm_snoc_split_seq (#c:_) (#eq:_) (add: CE.cm c eq) (s: seq c) (t: seq c{length s == length t}) (sum_seq: seq c{length sum_seq == length s}) (proof: (i: under (length s)) -> Lemma ((index s i `add.mult` index t i) `eq.eq` (index sum_seq i))) : Lemma ((foldm_snoc add s `add.mult` foldm_snoc add t) `eq.eq` (foldm_snoc add sum_seq))
val foldm_snoc_split_seq (#c:_) (#eq:_) (add: CE.cm c eq) (s: seq c) (t: seq c{length s == length t}) (sum_seq: seq c{length sum_seq == length s}) (proof: (i: under (length s)) -> Lemma ((index s i `add.mult` index t i) `eq.eq` (index sum_seq i))) : Lemma ((foldm_snoc add s `add.mult` foldm_snoc add t) `eq.eq` (foldm_snoc add sum_seq))
let foldm_snoc_split_seq #c #eq (add: CE.cm c eq) (s: seq c) (t: seq c{length s == length t}) (sum_seq: seq c{length sum_seq == length s}) (proof: (i: under (length s)) -> Lemma ((index s i `add.mult` index t i) `eq.eq` (index sum_seq i))) : Lemma ((foldm_snoc add s `add.mult` foldm_snoc add t) `eq.eq` (foldm_snoc add sum_seq)) = if length s = 0 then add.identity add.unit else let n = length s in let index_1 (i:under n) = index s i in let index_2 (i:under n) = index t i in let nk = (n-1) in assert (n == closed_interval_size 0 nk); let index_sum = func_sum add index_1 index_2 in let expr1 = init_func_from_expr #c #0 #(n-1) index_1 0 nk in let expr2 = init_func_from_expr #c #0 #(n-1) index_2 0 nk in let expr_sum = init_func_from_expr #c #0 #(n-1) index_sum 0 nk in Classical.forall_intro eq.reflexivity; Classical.forall_intro_2 (Classical.move_requires_2 eq.symmetry); Classical.forall_intro_3 (Classical.move_requires_3 eq.transitivity); foldm_snoc_split add 0 (n-1) index_1 index_2; assert (foldm_snoc add (init n (expr_sum)) `eq.eq` add.mult (foldm_snoc add (init n expr1)) (foldm_snoc add (init n expr2))); lemma_eq_elim s (init n expr1); lemma_eq_elim t (init n expr2); Classical.forall_intro proof; eq_of_seq_from_element_equality eq (init n expr_sum) sum_seq; foldm_snoc_equality add (init n expr_sum) sum_seq ; assert (eq.eq (foldm_snoc add (init n expr_sum)) (foldm_snoc add sum_seq)); assert (foldm_snoc add s == foldm_snoc add (init n expr1)); assert (foldm_snoc add t == foldm_snoc add (init n expr2)); assert (add.mult (foldm_snoc add s) (foldm_snoc add t) `eq.eq` foldm_snoc add (init n (expr_sum))); eq.transitivity (add.mult (foldm_snoc add s) (foldm_snoc add t)) (foldm_snoc add (init n (expr_sum))) (foldm_snoc add sum_seq)
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 696, "start_col": 0, "start_line": 658 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *) #push-options "--z3rlimit_factor 2 --fuel 1 --ifuel 0" let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0)) #pop-options //////////////////////////////////////////////////////////////////////////////// // foldm_snoc_split //////////////////////////////////////////////////////////////////////////////// (* Some utilities to introduce associativity-commutativity reasoning on CM using quantified formulas with patterns. Use these with care, since with large terms the SMT solver may end up with an explosion of instantiations *) let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y z:c). {:pattern (x `cm.mult` y `cm.mult` z)} (x `cm.mult` y `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) = Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity) let cm_commutativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y:c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x)) = Classical.forall_intro_2 (Classical.move_requires_2 cm.commutativity) (* A utility to introduce the equivalence relation laws into the context. FStar.Algebra.CommutativeMonoid provides something similar, but this version provides a more goal-directed pattern for transitivity. We should consider changing FStar.Algebra.CommutativeMonoid *) let elim_eq_laws #a (eq:CE.equiv a) : Lemma ( (forall x.{:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y.{:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z.{:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z) ) = CE.elim_eq_laws eq let fold_decomposition_aux #c #eq (cm: CE.cm c eq) (n0: int) (nk: int{nk=n0}) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Lemma (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))) = elim_eq_laws eq; let sum_of_funcs (i: under (closed_interval_size n0 nk)) = expr1 (n0+i) `cm.mult` expr2 (n0+i) in lemma_eq_elim (init (closed_interval_size n0 nk) sum_of_funcs) (create 1 (expr1 n0 `cm.mult` expr2 n0)); foldm_snoc_singleton cm (expr1 n0 `cm.mult` expr2 n0); let ts = (init (closed_interval_size n0 nk) sum_of_funcs) in let ts1 = (init (nk+1-n0) (fun i -> expr1 (n0+i))) in let ts2 = (init (nk+1-n0) (fun i -> expr2 (n0+i))) in assert (foldm_snoc cm ts `eq.eq` sum_of_funcs (nk-n0)); // this assert speeds up the proof. foldm_snoc_singleton cm (expr1 nk); foldm_snoc_singleton cm (expr2 nk); cm.congruence (foldm_snoc cm ts1) (foldm_snoc cm ts2) (expr1 nk) (expr2 nk) let aux_shuffle_lemma #c #eq (cm: CE.cm c eq) (s1 s2 l1 l2: c) : Lemma (((s1 `cm.mult` s2) `cm.mult` (l1 `cm.mult` l2)) `eq.eq` ((s1 `cm.mult` l1) `cm.mult` (s2 `cm.mult` l2))) = elim_eq_laws eq; cm_commutativity cm; cm_associativity cm; let (+) = cm.mult in cm.congruence (s1+s2) l1 (s2+s1) l1; cm.congruence ((s1+s2)+l1) l2 ((s2+s1)+l1) l2; cm.congruence ((s2+s1)+l1) l2 (s2+(s1+l1)) l2; cm.congruence (s2+(s1+l1)) l2 ((s1+l1)+s2) l2 #push-options "--ifuel 0 --fuel 1 --z3rlimit 40" (* This proof is quite delicate, for several reasons: - It's working with higher order functions that are non-trivially dependently typed, notably on the ranges the ranges of indexes they manipulate - When using the induction hypothesis (i.e., on a recursive call), what we get is a property about the function at a different type, i.e., `range_count n0 (nk - 1)`. - If left to the SMT solver alone, these higher order functions at slightly different types cannot be proven equal and the proof fails, often mysteriously. - To have something more robust, I rewrote this function to return a squash proof, and then to coerce this proof to the type needed, where the F* unififer/normalization machinery can help, rather than leaving it purely to SMT, which is what happens when the property is states as a postcondition of a Lemma *) let rec foldm_snoc_split' #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Tot (squash (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk))))) (decreases nk-n0) = if (nk=n0) then fold_decomposition_aux cm n0 nk expr1 expr2 else ( cm_commutativity cm; elim_eq_laws eq; let lfunc_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr (func_sum cm expr1 expr2) n0 nf in let range_count = closed_interval_size in let full_count = range_count n0 nk in let sub_count = range_count n0 (nk-1) in let fullseq = init full_count (lfunc_up_to nk) in let rfunc_1_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr1 n0 nf in let rfunc_2_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr2 n0 nf in let fullseq_r1 = init full_count (rfunc_1_up_to nk) in let fullseq_r2 = init full_count (rfunc_2_up_to nk) in let subseq = init sub_count (lfunc_up_to nk) in let subfold = foldm_snoc cm subseq in let last = lfunc_up_to nk sub_count in lemma_eq_elim (fst (un_snoc fullseq)) subseq; // subseq is literally (liat fullseq) let fullfold = foldm_snoc cm fullseq in let subseq_r1 = init sub_count (rfunc_1_up_to nk) in let subseq_r2 = init sub_count (rfunc_2_up_to nk) in lemma_eq_elim (fst (un_snoc fullseq_r1)) subseq_r1; // subseq is literally (liat fullseq) lemma_eq_elim (fst (un_snoc fullseq_r2)) subseq_r2; // subseq is literally (liat fullseq) lemma_eq_elim (init sub_count (lfunc_up_to nk)) subseq; lemma_eq_elim (init sub_count (lfunc_up_to (nk-1))) subseq; lemma_eq_elim subseq_r1 (init sub_count (rfunc_1_up_to (nk-1))); lemma_eq_elim subseq_r2 (init sub_count (rfunc_2_up_to (nk-1))); let fullfold_r1 = foldm_snoc cm fullseq_r1 in let fullfold_r2 = foldm_snoc cm fullseq_r2 in let subfold_r1 = foldm_snoc cm subseq_r1 in let subfold_r2 = foldm_snoc cm subseq_r2 in cm.congruence (foldm_snoc cm (init sub_count (rfunc_1_up_to (nk-1)))) (foldm_snoc cm (init sub_count (rfunc_2_up_to (nk-1)))) subfold_r1 subfold_r2; let last_r1 = rfunc_1_up_to nk sub_count in let last_r2 = rfunc_2_up_to nk sub_count in let nk' = nk - 1 in (* here's the nasty bit with where we have to massage the proof from the induction hypothesis *) let ih : squash ((foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')) `eq.eq` cm.mult (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))) (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))))) = foldm_snoc_split' cm n0 nk' expr1 expr2 in calc (==) { subfold; == { } foldm_snoc cm subseq; == { assert (Seq.equal subseq (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk'))) } foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')); }; assert (Seq.equal subseq_r1 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))); assert (Seq.equal subseq_r2 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))); let _ : squash (subfold `eq.eq` (subfold_r1 `cm.mult` subfold_r2)) = ih in cm.congruence subfold last (subfold_r1 `cm.mult` subfold_r2) last; aux_shuffle_lemma cm subfold_r1 subfold_r2 (rfunc_1_up_to nk sub_count) (rfunc_2_up_to nk sub_count); cm.congruence (subfold_r1 `cm.mult` (rfunc_1_up_to nk sub_count)) (subfold_r2 `cm.mult` (rfunc_2_up_to nk sub_count)) (foldm_snoc cm fullseq_r1) (foldm_snoc cm fullseq_r2) ) #pop-options /// Finally, package the proof up into a Lemma, as expected by the interface let foldm_snoc_split #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) = foldm_snoc_split' cm n0 nk expr1 expr2 open FStar.Seq.Equiv let rec foldm_snoc_equality #c #eq (add: CE.cm c eq) (s t: seq c) : Lemma (requires length s == length t /\ eq_of_seq eq s t) (ensures foldm_snoc add s `eq.eq` foldm_snoc add t) (decreases length s) = if length s = 0 then ( assert_norm (foldm_snoc add s == add.unit); assert_norm (foldm_snoc add t == add.unit); eq.reflexivity add.unit ) else ( let sliat, slast = un_snoc s in let tliat, tlast = un_snoc t in eq_of_seq_unsnoc eq (length s) s t; foldm_snoc_equality add sliat tliat; add.congruence slast (foldm_snoc add sliat) tlast (foldm_snoc add tliat) )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
add: FStar.Algebra.CommMonoid.Equiv.cm c eq -> s: FStar.Seq.Base.seq c -> t: FStar.Seq.Base.seq c {FStar.Seq.Base.length s == FStar.Seq.Base.length t} -> sum_seq: FStar.Seq.Base.seq c {FStar.Seq.Base.length sum_seq == FStar.Seq.Base.length s} -> proof: (i: FStar.IntegerIntervals.under (FStar.Seq.Base.length s) -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (CM?.mult add (FStar.Seq.Base.index s i) (FStar.Seq.Base.index t i)) (FStar.Seq.Base.index sum_seq i))) -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (CM?.mult add (FStar.Seq.Permutation.foldm_snoc add s) (FStar.Seq.Permutation.foldm_snoc add t)) (FStar.Seq.Permutation.foldm_snoc add sum_seq))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Seq.Base.seq", "Prims.eq2", "Prims.nat", "FStar.Seq.Base.length", "FStar.IntegerIntervals.under", "Prims.unit", "Prims.l_True", "Prims.squash", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "FStar.Seq.Base.index", "Prims.Nil", "FStar.Pervasives.pattern", "Prims.op_Equality", "Prims.int", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__identity", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit", "Prims.bool", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__transitivity", "FStar.Seq.Permutation.foldm_snoc", "FStar.Seq.Base.init", "Prims._assert", "FStar.Seq.Permutation.foldm_snoc_equality", "FStar.Seq.Equiv.eq_of_seq_from_element_equality", "FStar.Classical.forall_intro", "FStar.Seq.Base.lemma_eq_elim", "FStar.Seq.Permutation.foldm_snoc_split", "Prims.op_Subtraction", "FStar.Classical.forall_intro_3", "Prims.l_imp", "Prims.l_and", "FStar.Classical.move_requires_3", "FStar.Classical.forall_intro_2", "FStar.Classical.move_requires_2", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__symmetry", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__reflexivity", "FStar.IntegerIntervals.closed_interval_size", "FStar.Seq.Permutation.init_func_from_expr", "FStar.Seq.Permutation.func_sum" ]
[]
false
false
true
false
false
let foldm_snoc_split_seq #c #eq (add: CE.cm c eq) (s: seq c) (t: seq c {length s == length t}) (sum_seq: seq c {length sum_seq == length s}) (proof: (i: under (length s) -> Lemma (((index s i) `add.mult` (index t i)) `eq.eq` (index sum_seq i)))) : Lemma (((foldm_snoc add s) `add.mult` (foldm_snoc add t)) `eq.eq` (foldm_snoc add sum_seq)) =
if length s = 0 then add.identity add.unit else let n = length s in let index_1 (i: under n) = index s i in let index_2 (i: under n) = index t i in let nk = (n - 1) in assert (n == closed_interval_size 0 nk); let index_sum = func_sum add index_1 index_2 in let expr1 = init_func_from_expr #c #0 #(n - 1) index_1 0 nk in let expr2 = init_func_from_expr #c #0 #(n - 1) index_2 0 nk in let expr_sum = init_func_from_expr #c #0 #(n - 1) index_sum 0 nk in Classical.forall_intro eq.reflexivity; Classical.forall_intro_2 (Classical.move_requires_2 eq.symmetry); Classical.forall_intro_3 (Classical.move_requires_3 eq.transitivity); foldm_snoc_split add 0 (n - 1) index_1 index_2; assert ((foldm_snoc add (init n (expr_sum))) `eq.eq` (add.mult (foldm_snoc add (init n expr1)) (foldm_snoc add (init n expr2)))); lemma_eq_elim s (init n expr1); lemma_eq_elim t (init n expr2); Classical.forall_intro proof; eq_of_seq_from_element_equality eq (init n expr_sum) sum_seq; foldm_snoc_equality add (init n expr_sum) sum_seq; assert (eq.eq (foldm_snoc add (init n expr_sum)) (foldm_snoc add sum_seq)); assert (foldm_snoc add s == foldm_snoc add (init n expr1)); assert (foldm_snoc add t == foldm_snoc add (init n expr2)); assert ((add.mult (foldm_snoc add s) (foldm_snoc add t)) `eq.eq` (foldm_snoc add (init n (expr_sum)))); eq.transitivity (add.mult (foldm_snoc add s) (foldm_snoc add t)) (foldm_snoc add (init n (expr_sum))) (foldm_snoc add sum_seq)
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.mk_to_p256_prime_comm_monoid
val mk_to_p256_prime_comm_monoid:BE.to_comm_monoid U64 4ul 0ul
val mk_to_p256_prime_comm_monoid:BE.to_comm_monoid U64 4ul 0ul
let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; }
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 1, "end_line": 44, "start_col": 0, "start_line": 38 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a)
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
Hacl.Impl.Exponentiation.Definitions.to_comm_monoid Lib.IntTypes.U64 (4ul <: FStar.UInt32.t) (0ul <: FStar.UInt32.t)
Prims.Tot
[ "total" ]
[]
[ "Hacl.Impl.Exponentiation.Definitions.Mkto_comm_monoid", "Lib.IntTypes.U64", "FStar.UInt32.uint_to_t", "Spec.P256.PointOps.felem", "Hacl.Spec.P256.Finv.nat_mod_comm_monoid", "Hacl.Impl.P256.Finv.linv_ctx", "Hacl.Impl.P256.Finv.linv", "Hacl.Impl.P256.Finv.refl" ]
[]
false
false
false
false
false
let mk_to_p256_prime_comm_monoid:BE.to_comm_monoid U64 4ul 0ul =
{ BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl }
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.mk_p256_prime_concrete_ops
val mk_p256_prime_concrete_ops:BE.concrete_ops U64 4ul 0ul
val mk_p256_prime_concrete_ops:BE.concrete_ops U64 4ul 0ul
let mk_p256_prime_concrete_ops : BE.concrete_ops U64 4ul 0ul = { BE.to = mk_to_p256_prime_comm_monoid; BE.lone = one_mod; BE.lmul = mul_mod; BE.lsqr = sqr_mod; }
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 1, "end_line": 68, "start_col": 0, "start_line": 63 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let one_mod ctx one = make_fone one inline_for_extraction noextract val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let mul_mod ctx x y xy = fmul xy x y inline_for_extraction noextract val sqr_mod : BE.lsqr_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let sqr_mod ctx x xx = fsqr xx x
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
Hacl.Impl.Exponentiation.Definitions.concrete_ops Lib.IntTypes.U64 (4ul <: FStar.UInt32.t) (0ul <: FStar.UInt32.t)
Prims.Tot
[ "total" ]
[]
[ "Hacl.Impl.Exponentiation.Definitions.Mkconcrete_ops", "Lib.IntTypes.U64", "FStar.UInt32.uint_to_t", "FStar.Ghost.hide", "Hacl.Impl.Exponentiation.Definitions.to_comm_monoid", "Hacl.Impl.P256.Finv.mk_to_p256_prime_comm_monoid", "Hacl.Impl.P256.Finv.one_mod", "Hacl.Impl.P256.Finv.mul_mod", "Hacl.Impl.P256.Finv.sqr_mod" ]
[]
false
false
false
false
false
let mk_p256_prime_concrete_ops:BE.concrete_ops U64 4ul 0ul =
{ BE.to = mk_to_p256_prime_comm_monoid; BE.lone = one_mod; BE.lmul = mul_mod; BE.lsqr = sqr_mod }
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.linv_ctx
val linv_ctx (a: LSeq.lseq uint64 0) : Type0
val linv_ctx (a: LSeq.lseq uint64 0) : Type0
let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 50, "end_line": 26, "start_col": 0, "start_line": 26 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0"
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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: Lib.Sequence.lseq Lib.IntTypes.uint64 0 -> Type0
Prims.Tot
[ "total" ]
[]
[ "Lib.Sequence.lseq", "Lib.IntTypes.uint64", "Prims.l_True" ]
[]
false
false
false
false
true
let linv_ctx (a: LSeq.lseq uint64 0) : Type0 =
True
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.fsquare_times_in_place
val fsquare_times_in_place (out:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ as_nat h out < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 out) (v b))
val fsquare_times_in_place (out:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ as_nat h out < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 out) (v b))
let fsquare_times_in_place out b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 out) (v b); BE.lexp_pow2_in_place 4ul 0ul mk_p256_prime_concrete_ops (null uint64) out b
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 78, "end_line": 82, "start_col": 0, "start_line": 79 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let one_mod ctx one = make_fone one inline_for_extraction noextract val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let mul_mod ctx x y xy = fmul xy x y inline_for_extraction noextract val sqr_mod : BE.lsqr_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let sqr_mod ctx x xx = fsqr xx x inline_for_extraction noextract let mk_p256_prime_concrete_ops : BE.concrete_ops U64 4ul 0ul = { BE.to = mk_to_p256_prime_comm_monoid; BE.lone = one_mod; BE.lmul = mul_mod; BE.lsqr = sqr_mod; } inline_for_extraction noextract val fsquare_times_in_place (out:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ as_nat h out < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 out) (v b))
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
out: Hacl.Impl.P256.Bignum.felem -> b: Lib.IntTypes.size_t -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Impl.P256.Bignum.felem", "Lib.IntTypes.size_t", "Hacl.Impl.Exponentiation.lexp_pow2_in_place", "Lib.IntTypes.U64", "FStar.UInt32.__uint_to_t", "Hacl.Impl.P256.Finv.mk_p256_prime_concrete_ops", "Lib.Buffer.null", "Lib.Buffer.MUT", "Lib.IntTypes.uint64", "Prims.unit", "Spec.Exponentiation.exp_pow2_lemma", "Spec.P256.PointOps.felem", "Hacl.Spec.P256.Finv.mk_nat_mod_concrete_ops", "Hacl.Impl.P256.Field.fmont_as_nat", "Lib.IntTypes.v", "Lib.IntTypes.U32", "Lib.IntTypes.PUB", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get" ]
[]
false
true
false
false
false
let fsquare_times_in_place out b =
let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 out) (v b); BE.lexp_pow2_in_place 4ul 0ul mk_p256_prime_concrete_ops (null uint64) out b
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.linv
val linv (a: LSeq.lseq uint64 4) : Type0
val linv (a: LSeq.lseq uint64 4) : Type0
let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 21, "end_line": 30, "start_col": 0, "start_line": 29 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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: Lib.Sequence.lseq Lib.IntTypes.uint64 4 -> Type0
Prims.Tot
[ "total" ]
[]
[ "Lib.Sequence.lseq", "Lib.IntTypes.uint64", "Prims.b2t", "Prims.op_LessThan", "Hacl.Spec.Bignum.Definitions.bn_v", "Lib.IntTypes.U64", "Spec.P256.PointOps.prime" ]
[]
false
false
false
false
true
let linv (a: LSeq.lseq uint64 4) : Type0 =
BD.bn_v a < S.prime
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.fsquare_times
val fsquare_times (out a:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ live h a /\ disjoint out a /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 a) (v b))
val fsquare_times (out a:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ live h a /\ disjoint out a /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 a) (v b))
let fsquare_times out a b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 a) (v b); BE.lexp_pow2 4ul 0ul mk_p256_prime_concrete_ops (null uint64) a b out
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 71, "end_line": 97, "start_col": 0, "start_line": 94 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let one_mod ctx one = make_fone one inline_for_extraction noextract val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let mul_mod ctx x y xy = fmul xy x y inline_for_extraction noextract val sqr_mod : BE.lsqr_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let sqr_mod ctx x xx = fsqr xx x inline_for_extraction noextract let mk_p256_prime_concrete_ops : BE.concrete_ops U64 4ul 0ul = { BE.to = mk_to_p256_prime_comm_monoid; BE.lone = one_mod; BE.lmul = mul_mod; BE.lsqr = sqr_mod; } inline_for_extraction noextract val fsquare_times_in_place (out:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ as_nat h out < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 out) (v b)) let fsquare_times_in_place out b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 out) (v b); BE.lexp_pow2_in_place 4ul 0ul mk_p256_prime_concrete_ops (null uint64) out b inline_for_extraction noextract val fsquare_times (out a:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ live h a /\ disjoint out a /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 a) (v b))
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
out: Hacl.Impl.P256.Bignum.felem -> a: Hacl.Impl.P256.Bignum.felem -> b: Lib.IntTypes.size_t -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Impl.P256.Bignum.felem", "Lib.IntTypes.size_t", "Hacl.Impl.Exponentiation.lexp_pow2", "Lib.IntTypes.U64", "FStar.UInt32.__uint_to_t", "Hacl.Impl.P256.Finv.mk_p256_prime_concrete_ops", "Lib.Buffer.null", "Lib.Buffer.MUT", "Lib.IntTypes.uint64", "Prims.unit", "Spec.Exponentiation.exp_pow2_lemma", "Spec.P256.PointOps.felem", "Hacl.Spec.P256.Finv.mk_nat_mod_concrete_ops", "Hacl.Impl.P256.Field.fmont_as_nat", "Lib.IntTypes.v", "Lib.IntTypes.U32", "Lib.IntTypes.PUB", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get" ]
[]
false
true
false
false
false
let fsquare_times out a b =
let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 a) (v b); BE.lexp_pow2 4ul 0ul mk_p256_prime_concrete_ops (null uint64) a b out
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.one_mod
val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid
val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid
let one_mod ctx one = make_fone one
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 35, "end_line": 49, "start_col": 0, "start_line": 49 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
Hacl.Impl.Exponentiation.Definitions.lone_st Lib.IntTypes.U64 4ul 0ul Hacl.Impl.P256.Finv.mk_to_p256_prime_comm_monoid
Prims.Tot
[ "total" ]
[]
[ "Lib.Buffer.lbuffer", "Lib.IntTypes.uint_t", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "FStar.UInt32.__uint_to_t", "Hacl.Impl.P256.Field.make_fone", "Prims.unit" ]
[]
false
false
false
false
false
let one_mod ctx one =
make_fone one
false
LowParseWriters.fsti
LowParseWriters.memory_invariant_includes
val memory_invariant_includes : ol: LowParseWriters.memory_invariant -> ne: LowParseWriters.memory_invariant -> Prims.logical
let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 38, "end_line": 45, "start_col": 0, "start_line": 42 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
ol: LowParseWriters.memory_invariant -> ne: LowParseWriters.memory_invariant -> Prims.logical
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.memory_invariant", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.loc", "LowParseWriters.__proj__Mkmemory_invariant__item__lwrite", "FStar.Monotonic.HyperStack.mem", "LowParseWriters.__proj__Mkmemory_invariant__item__h0", "Prims.b2t", "FStar.Monotonic.HyperHeap.includes", "FStar.Monotonic.HyperStack.get_tip", "LowStar.Monotonic.Buffer.loc_includes", "Prims.logical" ]
[]
false
false
false
true
true
let memory_invariant_includes (ol ne: memory_invariant) =
B.modifies ol.lwrite ol.h0 ne.h0 /\ (HS.get_tip ol.h0) `HS.includes` (HS.get_tip ne.h0) /\ ol.lwrite `B.loc_includes` ne.lwrite
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.new_invariant
val new_invariant (#opened_invariants:inames) (p:vprop) : SteelAtomicUT (inv p) opened_invariants p (fun _ -> emp)
val new_invariant (#opened_invariants:inames) (p:vprop) : SteelAtomicUT (inv p) opened_invariants p (fun _ -> emp)
let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 74, "end_line": 639, "start_col": 0, "start_line": 639 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Steel.Effect.Common.vprop -> Steel.Effect.Atomic.SteelAtomicUT (Steel.Effect.Common.inv p)
Steel.Effect.Atomic.SteelAtomicUT
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Atomic.return", "Steel.Effect.Common.inv", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit", "Steel.Effect.Common.req", "Steel.Effect.Common.rm", "Steel.Effect.Atomic.fresh_inv", "Prims.Nil", "Steel.Memory.pre_inv", "Steel.Effect.Atomic.fresh_invariant" ]
[]
false
true
false
false
false
let new_invariant #uses p =
let i = fresh_invariant #uses p [] in return i
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc_of_equal_inits
val foldm_snoc_of_equal_inits (#c:_) (#eq:_) (#m: pos) (cm: CE.cm c eq) (f: (under m) -> c) (g: (under m) -> c) : Lemma (requires (forall (i: under m). f i `eq.eq` g i)) (ensures foldm_snoc cm (init m f) `eq.eq` foldm_snoc cm (init m g))
val foldm_snoc_of_equal_inits (#c:_) (#eq:_) (#m: pos) (cm: CE.cm c eq) (f: (under m) -> c) (g: (under m) -> c) : Lemma (requires (forall (i: under m). f i `eq.eq` g i)) (ensures foldm_snoc cm (init m f) `eq.eq` foldm_snoc cm (init m g))
let rec foldm_snoc_of_equal_inits #c #eq #m (cm: CE.cm c eq) (f: (under m) -> c) (g: (under m) -> c) : Lemma (requires (forall (i: under m). f i `eq.eq` g i)) (ensures foldm_snoc cm (init m f) `eq.eq` foldm_snoc cm (init m g)) = if m=0 then begin assert_norm (foldm_snoc cm (init m f) == cm.unit); assert_norm (foldm_snoc cm (init m g) == cm.unit); eq.reflexivity cm.unit end else if m=1 then begin foldm_snoc_singleton cm (f 0); foldm_snoc_singleton cm (g 0); eq.transitivity (foldm_snoc cm (init m f)) (f 0) (g 0); eq.symmetry (foldm_snoc cm (init m g)) (g 0); eq.transitivity (foldm_snoc cm (init m f)) (g 0) (foldm_snoc cm (init m g)) end else let fliat, flast = un_snoc (init m f) in let gliat, glast = un_snoc (init m g) in foldm_snoc_of_equal_inits cm (fun (i: under (m-1)) -> f i) (fun (i: under (m-1)) -> g i); lemma_eq_elim (init (m-1) (fun (i: under (m-1)) -> f i)) fliat; lemma_eq_elim (init (m-1) (fun (i: under (m-1)) -> g i)) gliat; cm.congruence flast (foldm_snoc cm fliat) glast (foldm_snoc cm gliat)
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 725, "start_col": 0, "start_line": 698 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *) #push-options "--z3rlimit_factor 2 --fuel 1 --ifuel 0" let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0)) #pop-options //////////////////////////////////////////////////////////////////////////////// // foldm_snoc_split //////////////////////////////////////////////////////////////////////////////// (* Some utilities to introduce associativity-commutativity reasoning on CM using quantified formulas with patterns. Use these with care, since with large terms the SMT solver may end up with an explosion of instantiations *) let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y z:c). {:pattern (x `cm.mult` y `cm.mult` z)} (x `cm.mult` y `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) = Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity) let cm_commutativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y:c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x)) = Classical.forall_intro_2 (Classical.move_requires_2 cm.commutativity) (* A utility to introduce the equivalence relation laws into the context. FStar.Algebra.CommutativeMonoid provides something similar, but this version provides a more goal-directed pattern for transitivity. We should consider changing FStar.Algebra.CommutativeMonoid *) let elim_eq_laws #a (eq:CE.equiv a) : Lemma ( (forall x.{:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y.{:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z.{:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z) ) = CE.elim_eq_laws eq let fold_decomposition_aux #c #eq (cm: CE.cm c eq) (n0: int) (nk: int{nk=n0}) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Lemma (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))) = elim_eq_laws eq; let sum_of_funcs (i: under (closed_interval_size n0 nk)) = expr1 (n0+i) `cm.mult` expr2 (n0+i) in lemma_eq_elim (init (closed_interval_size n0 nk) sum_of_funcs) (create 1 (expr1 n0 `cm.mult` expr2 n0)); foldm_snoc_singleton cm (expr1 n0 `cm.mult` expr2 n0); let ts = (init (closed_interval_size n0 nk) sum_of_funcs) in let ts1 = (init (nk+1-n0) (fun i -> expr1 (n0+i))) in let ts2 = (init (nk+1-n0) (fun i -> expr2 (n0+i))) in assert (foldm_snoc cm ts `eq.eq` sum_of_funcs (nk-n0)); // this assert speeds up the proof. foldm_snoc_singleton cm (expr1 nk); foldm_snoc_singleton cm (expr2 nk); cm.congruence (foldm_snoc cm ts1) (foldm_snoc cm ts2) (expr1 nk) (expr2 nk) let aux_shuffle_lemma #c #eq (cm: CE.cm c eq) (s1 s2 l1 l2: c) : Lemma (((s1 `cm.mult` s2) `cm.mult` (l1 `cm.mult` l2)) `eq.eq` ((s1 `cm.mult` l1) `cm.mult` (s2 `cm.mult` l2))) = elim_eq_laws eq; cm_commutativity cm; cm_associativity cm; let (+) = cm.mult in cm.congruence (s1+s2) l1 (s2+s1) l1; cm.congruence ((s1+s2)+l1) l2 ((s2+s1)+l1) l2; cm.congruence ((s2+s1)+l1) l2 (s2+(s1+l1)) l2; cm.congruence (s2+(s1+l1)) l2 ((s1+l1)+s2) l2 #push-options "--ifuel 0 --fuel 1 --z3rlimit 40" (* This proof is quite delicate, for several reasons: - It's working with higher order functions that are non-trivially dependently typed, notably on the ranges the ranges of indexes they manipulate - When using the induction hypothesis (i.e., on a recursive call), what we get is a property about the function at a different type, i.e., `range_count n0 (nk - 1)`. - If left to the SMT solver alone, these higher order functions at slightly different types cannot be proven equal and the proof fails, often mysteriously. - To have something more robust, I rewrote this function to return a squash proof, and then to coerce this proof to the type needed, where the F* unififer/normalization machinery can help, rather than leaving it purely to SMT, which is what happens when the property is states as a postcondition of a Lemma *) let rec foldm_snoc_split' #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Tot (squash (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk))))) (decreases nk-n0) = if (nk=n0) then fold_decomposition_aux cm n0 nk expr1 expr2 else ( cm_commutativity cm; elim_eq_laws eq; let lfunc_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr (func_sum cm expr1 expr2) n0 nf in let range_count = closed_interval_size in let full_count = range_count n0 nk in let sub_count = range_count n0 (nk-1) in let fullseq = init full_count (lfunc_up_to nk) in let rfunc_1_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr1 n0 nf in let rfunc_2_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr2 n0 nf in let fullseq_r1 = init full_count (rfunc_1_up_to nk) in let fullseq_r2 = init full_count (rfunc_2_up_to nk) in let subseq = init sub_count (lfunc_up_to nk) in let subfold = foldm_snoc cm subseq in let last = lfunc_up_to nk sub_count in lemma_eq_elim (fst (un_snoc fullseq)) subseq; // subseq is literally (liat fullseq) let fullfold = foldm_snoc cm fullseq in let subseq_r1 = init sub_count (rfunc_1_up_to nk) in let subseq_r2 = init sub_count (rfunc_2_up_to nk) in lemma_eq_elim (fst (un_snoc fullseq_r1)) subseq_r1; // subseq is literally (liat fullseq) lemma_eq_elim (fst (un_snoc fullseq_r2)) subseq_r2; // subseq is literally (liat fullseq) lemma_eq_elim (init sub_count (lfunc_up_to nk)) subseq; lemma_eq_elim (init sub_count (lfunc_up_to (nk-1))) subseq; lemma_eq_elim subseq_r1 (init sub_count (rfunc_1_up_to (nk-1))); lemma_eq_elim subseq_r2 (init sub_count (rfunc_2_up_to (nk-1))); let fullfold_r1 = foldm_snoc cm fullseq_r1 in let fullfold_r2 = foldm_snoc cm fullseq_r2 in let subfold_r1 = foldm_snoc cm subseq_r1 in let subfold_r2 = foldm_snoc cm subseq_r2 in cm.congruence (foldm_snoc cm (init sub_count (rfunc_1_up_to (nk-1)))) (foldm_snoc cm (init sub_count (rfunc_2_up_to (nk-1)))) subfold_r1 subfold_r2; let last_r1 = rfunc_1_up_to nk sub_count in let last_r2 = rfunc_2_up_to nk sub_count in let nk' = nk - 1 in (* here's the nasty bit with where we have to massage the proof from the induction hypothesis *) let ih : squash ((foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')) `eq.eq` cm.mult (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))) (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))))) = foldm_snoc_split' cm n0 nk' expr1 expr2 in calc (==) { subfold; == { } foldm_snoc cm subseq; == { assert (Seq.equal subseq (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk'))) } foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')); }; assert (Seq.equal subseq_r1 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))); assert (Seq.equal subseq_r2 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))); let _ : squash (subfold `eq.eq` (subfold_r1 `cm.mult` subfold_r2)) = ih in cm.congruence subfold last (subfold_r1 `cm.mult` subfold_r2) last; aux_shuffle_lemma cm subfold_r1 subfold_r2 (rfunc_1_up_to nk sub_count) (rfunc_2_up_to nk sub_count); cm.congruence (subfold_r1 `cm.mult` (rfunc_1_up_to nk sub_count)) (subfold_r2 `cm.mult` (rfunc_2_up_to nk sub_count)) (foldm_snoc cm fullseq_r1) (foldm_snoc cm fullseq_r2) ) #pop-options /// Finally, package the proof up into a Lemma, as expected by the interface let foldm_snoc_split #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) = foldm_snoc_split' cm n0 nk expr1 expr2 open FStar.Seq.Equiv let rec foldm_snoc_equality #c #eq (add: CE.cm c eq) (s t: seq c) : Lemma (requires length s == length t /\ eq_of_seq eq s t) (ensures foldm_snoc add s `eq.eq` foldm_snoc add t) (decreases length s) = if length s = 0 then ( assert_norm (foldm_snoc add s == add.unit); assert_norm (foldm_snoc add t == add.unit); eq.reflexivity add.unit ) else ( let sliat, slast = un_snoc s in let tliat, tlast = un_snoc t in eq_of_seq_unsnoc eq (length s) s t; foldm_snoc_equality add sliat tliat; add.congruence slast (foldm_snoc add sliat) tlast (foldm_snoc add tliat) ) let foldm_snoc_split_seq #c #eq (add: CE.cm c eq) (s: seq c) (t: seq c{length s == length t}) (sum_seq: seq c{length sum_seq == length s}) (proof: (i: under (length s)) -> Lemma ((index s i `add.mult` index t i) `eq.eq` (index sum_seq i))) : Lemma ((foldm_snoc add s `add.mult` foldm_snoc add t) `eq.eq` (foldm_snoc add sum_seq)) = if length s = 0 then add.identity add.unit else let n = length s in let index_1 (i:under n) = index s i in let index_2 (i:under n) = index t i in let nk = (n-1) in assert (n == closed_interval_size 0 nk); let index_sum = func_sum add index_1 index_2 in let expr1 = init_func_from_expr #c #0 #(n-1) index_1 0 nk in let expr2 = init_func_from_expr #c #0 #(n-1) index_2 0 nk in let expr_sum = init_func_from_expr #c #0 #(n-1) index_sum 0 nk in Classical.forall_intro eq.reflexivity; Classical.forall_intro_2 (Classical.move_requires_2 eq.symmetry); Classical.forall_intro_3 (Classical.move_requires_3 eq.transitivity); foldm_snoc_split add 0 (n-1) index_1 index_2; assert (foldm_snoc add (init n (expr_sum)) `eq.eq` add.mult (foldm_snoc add (init n expr1)) (foldm_snoc add (init n expr2))); lemma_eq_elim s (init n expr1); lemma_eq_elim t (init n expr2); Classical.forall_intro proof; eq_of_seq_from_element_equality eq (init n expr_sum) sum_seq; foldm_snoc_equality add (init n expr_sum) sum_seq ; assert (eq.eq (foldm_snoc add (init n expr_sum)) (foldm_snoc add sum_seq)); assert (foldm_snoc add s == foldm_snoc add (init n expr1)); assert (foldm_snoc add t == foldm_snoc add (init n expr2)); assert (add.mult (foldm_snoc add s) (foldm_snoc add t) `eq.eq` foldm_snoc add (init n (expr_sum))); eq.transitivity (add.mult (foldm_snoc add s) (foldm_snoc add t)) (foldm_snoc add (init n (expr_sum))) (foldm_snoc add sum_seq)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cm: FStar.Algebra.CommMonoid.Equiv.cm c eq -> f: (_: FStar.IntegerIntervals.under m -> c) -> g: (_: FStar.IntegerIntervals.under m -> c) -> FStar.Pervasives.Lemma (requires forall (i: FStar.IntegerIntervals.under m). EQ?.eq eq (f i) (g i)) (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init m f)) (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init m g)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "Prims.pos", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.IntegerIntervals.under", "Prims.op_Equality", "Prims.int", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__reflexivity", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit", "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.eq2", "FStar.Seq.Permutation.foldm_snoc", "FStar.Seq.Base.init", "Prims.bool", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__transitivity", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__symmetry", "FStar.Seq.Permutation.foldm_snoc_singleton", "FStar.Seq.Base.seq", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__congruence", "FStar.Seq.Base.lemma_eq_elim", "Prims.op_Subtraction", "FStar.Seq.Permutation.foldm_snoc_of_equal_inits", "FStar.Pervasives.Native.tuple2", "FStar.Seq.Properties.snoc", "FStar.Pervasives.Native.fst", "FStar.Pervasives.Native.snd", "FStar.Seq.Properties.un_snoc", "Prims.l_Forall", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec foldm_snoc_of_equal_inits #c #eq #m (cm: CE.cm c eq) (f: ((under m) -> c)) (g: ((under m) -> c)) : Lemma (requires (forall (i: under m). (f i) `eq.eq` (g i))) (ensures (foldm_snoc cm (init m f)) `eq.eq` (foldm_snoc cm (init m g))) =
if m = 0 then (assert_norm (foldm_snoc cm (init m f) == cm.unit); assert_norm (foldm_snoc cm (init m g) == cm.unit); eq.reflexivity cm.unit) else if m = 1 then (foldm_snoc_singleton cm (f 0); foldm_snoc_singleton cm (g 0); eq.transitivity (foldm_snoc cm (init m f)) (f 0) (g 0); eq.symmetry (foldm_snoc cm (init m g)) (g 0); eq.transitivity (foldm_snoc cm (init m f)) (g 0) (foldm_snoc cm (init m g))) else let fliat, flast = un_snoc (init m f) in let gliat, glast = un_snoc (init m g) in foldm_snoc_of_equal_inits cm (fun (i: under (m - 1)) -> f i) (fun (i: under (m - 1)) -> g i); lemma_eq_elim (init (m - 1) (fun (i: under (m - 1)) -> f i)) fliat; lemma_eq_elim (init (m - 1) (fun (i: under (m - 1)) -> g i)) gliat; cm.congruence flast (foldm_snoc cm fliat) glast (foldm_snoc cm gliat)
false
LowParseWriters.fsti
LowParseWriters.read_return
val read_return (a: Type) (x: a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv)
val read_return (a: Type) (x: a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv)
let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 39, "end_line": 160, "start_col": 0, "start_line": 157 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> x: a -> inv: LowParseWriters.memory_invariant -> LowParseWriters.read_repr a Prims.l_True (fun res -> res == x) (fun _ -> Prims.l_False) inv
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.memory_invariant", "LowParseWriters.ReadRepr", "Prims.l_True", "Prims.eq2", "Prims.unit", "Prims.l_False", "LowParseWriters.read_return_spec", "LowParseWriters.read_return_impl", "LowParseWriters.read_repr" ]
[]
false
false
false
false
false
let read_return (a: Type) (x: a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) =
ReadRepr _ (read_return_impl a x inv)
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.sqr_mod
val sqr_mod : BE.lsqr_st U64 4ul 0ul mk_to_p256_prime_comm_monoid
val sqr_mod : BE.lsqr_st U64 4ul 0ul mk_to_p256_prime_comm_monoid
let sqr_mod ctx x xx = fsqr xx x
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 32, "end_line": 59, "start_col": 0, "start_line": 59 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let one_mod ctx one = make_fone one inline_for_extraction noextract val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let mul_mod ctx x y xy = fmul xy x y inline_for_extraction noextract
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
Hacl.Impl.Exponentiation.Definitions.lsqr_st Lib.IntTypes.U64 4ul 0ul Hacl.Impl.P256.Finv.mk_to_p256_prime_comm_monoid
Prims.Tot
[ "total" ]
[]
[ "Lib.Buffer.lbuffer", "Lib.IntTypes.uint_t", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "FStar.UInt32.__uint_to_t", "Hacl.Impl.P256.Field.fsqr", "Prims.unit" ]
[]
false
false
false
false
false
let sqr_mod ctx x xx =
fsqr xx x
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.finv_256
val finv_256 (x256 x2 x30 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h x256 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a x256 /\ disjoint x30 x2 /\ disjoint x30 x256 /\ disjoint x2 x256 /\ as_nat h a < S.prime /\ as_nat h x30 < S.prime /\ as_nat h x2 < S.prime) (ensures fun h0 _ h1 -> modifies (loc x256 |+| loc x2) h0 h1 /\ as_nat h1 x256 < S.prime /\ (let f = fmont_as_nat h0 a in let x30 = fmont_as_nat h0 x30 in let x2 = fmont_as_nat h0 x2 in let x32_s = S.fmul (SI.fsquare_times x30 2) x2 in let x64_s = S.fmul (SI.fsquare_times x32_s 32) f in let x192_s = S.fmul (SI.fsquare_times x64_s 128) x32_s in let x224_s = S.fmul (SI.fsquare_times x192_s 32) x32_s in let x254_s = S.fmul (SI.fsquare_times x224_s 30) x30 in let x256_s = S.fmul (SI.fsquare_times x254_s 2) f in fmont_as_nat h1 x256 = x256_s))
val finv_256 (x256 x2 x30 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h x256 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a x256 /\ disjoint x30 x2 /\ disjoint x30 x256 /\ disjoint x2 x256 /\ as_nat h a < S.prime /\ as_nat h x30 < S.prime /\ as_nat h x2 < S.prime) (ensures fun h0 _ h1 -> modifies (loc x256 |+| loc x2) h0 h1 /\ as_nat h1 x256 < S.prime /\ (let f = fmont_as_nat h0 a in let x30 = fmont_as_nat h0 x30 in let x2 = fmont_as_nat h0 x2 in let x32_s = S.fmul (SI.fsquare_times x30 2) x2 in let x64_s = S.fmul (SI.fsquare_times x32_s 32) f in let x192_s = S.fmul (SI.fsquare_times x64_s 128) x32_s in let x224_s = S.fmul (SI.fsquare_times x192_s 32) x32_s in let x254_s = S.fmul (SI.fsquare_times x224_s 30) x30 in let x256_s = S.fmul (SI.fsquare_times x254_s 2) f in fmont_as_nat h1 x256 = x256_s))
let finv_256 x256 x2 x30 a = let h0 = ST.get () in fsquare_times x256 x30 2ul; fmul x256 x256 x2; let h1 = ST.get () in assert (fmont_as_nat h1 x256 == // x32 S.fmul (SI.fsquare_times (fmont_as_nat h0 x30) 2) (fmont_as_nat h0 x2)); fsquare_times x2 x256 32ul; fmul x2 x2 a; let h2 = ST.get () in assert (fmont_as_nat h2 x2 == // x64 S.fmul (SI.fsquare_times (fmont_as_nat h1 x256) 32) (fmont_as_nat h0 a)); fsquare_times_in_place x2 128ul; fmul x2 x2 x256; let h3 = ST.get () in assert (fmont_as_nat h3 x2 == // x192 S.fmul (SI.fsquare_times (fmont_as_nat h2 x2) 128) (fmont_as_nat h1 x256)); fsquare_times_in_place x2 32ul; fmul x2 x2 x256; let h4 = ST.get () in assert (fmont_as_nat h4 x2 == // x224 S.fmul (SI.fsquare_times (fmont_as_nat h3 x2) 32) (fmont_as_nat h1 x256)); fsquare_times_in_place x2 30ul; fmul x2 x2 x30; let h5 = ST.get () in assert (fmont_as_nat h5 x2 == // x254 S.fmul (SI.fsquare_times (fmont_as_nat h4 x2) 30) (fmont_as_nat h0 x30)); fsquare_times_in_place x2 2ul; fmul x256 x2 a; let h6 = ST.get () in assert (fmont_as_nat h6 x256 == // x256 S.fmul (SI.fsquare_times (fmont_as_nat h5 x2) 2) (fmont_as_nat h0 a))
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 73, "end_line": 214, "start_col": 0, "start_line": 178 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let one_mod ctx one = make_fone one inline_for_extraction noextract val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let mul_mod ctx x y xy = fmul xy x y inline_for_extraction noextract val sqr_mod : BE.lsqr_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let sqr_mod ctx x xx = fsqr xx x inline_for_extraction noextract let mk_p256_prime_concrete_ops : BE.concrete_ops U64 4ul 0ul = { BE.to = mk_to_p256_prime_comm_monoid; BE.lone = one_mod; BE.lmul = mul_mod; BE.lsqr = sqr_mod; } inline_for_extraction noextract val fsquare_times_in_place (out:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ as_nat h out < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 out) (v b)) let fsquare_times_in_place out b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 out) (v b); BE.lexp_pow2_in_place 4ul 0ul mk_p256_prime_concrete_ops (null uint64) out b inline_for_extraction noextract val fsquare_times (out a:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ live h a /\ disjoint out a /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 a) (v b)) let fsquare_times out a b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 a) (v b); BE.lexp_pow2 4ul 0ul mk_p256_prime_concrete_ops (null uint64) a b out inline_for_extraction noextract val finv_30 (x30 x2 tmp1 tmp2 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h tmp1 /\ live h tmp2 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint x30 x2 /\ disjoint x30 tmp1 /\ disjoint x30 tmp2 /\ disjoint x2 tmp1 /\ disjoint x2 tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc x30 |+| loc x2 |+| loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 x30 < S.prime /\ as_nat h1 x2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2_s = S.fmul (SI.fsquare_times f 1) f in let x3_s = S.fmul (SI.fsquare_times x2_s 1) f in let x6_s = S.fmul (SI.fsquare_times x3_s 3) x3_s in let x12_s = S.fmul (SI.fsquare_times x6_s 6) x6_s in let x15_s = S.fmul (SI.fsquare_times x12_s 3) x3_s in let x30_s = S.fmul (SI.fsquare_times x15_s 15) x15_s in fmont_as_nat h1 x30 = x30_s /\ fmont_as_nat h1 x2 = x2_s)) let finv_30 x30 x2 tmp1 tmp2 a = let h0 = ST.get () in fsquare_times x2 a 1ul; fmul x2 x2 a; let h1 = ST.get () in assert (fmont_as_nat h1 x2 == S.fmul (SI.fsquare_times (fmont_as_nat h0 a) 1) (fmont_as_nat h0 a)); fsquare_times x30 x2 1ul; fmul x30 x30 a; let h2 = ST.get () in assert (fmont_as_nat h2 x30 == // x3 S.fmul (SI.fsquare_times (fmont_as_nat h1 x2) 1) (fmont_as_nat h0 a)); fsquare_times tmp1 x30 3ul; fmul tmp1 tmp1 x30; let h3 = ST.get () in assert (fmont_as_nat h3 tmp1 == // x6 S.fmul (SI.fsquare_times (fmont_as_nat h2 x30) 3) (fmont_as_nat h2 x30)); fsquare_times tmp2 tmp1 6ul; fmul tmp2 tmp2 tmp1; let h4 = ST.get () in assert (fmont_as_nat h4 tmp2 == // x12 S.fmul (SI.fsquare_times (fmont_as_nat h3 tmp1) 6) (fmont_as_nat h3 tmp1)); fsquare_times tmp1 tmp2 3ul; fmul tmp1 tmp1 x30; let h5 = ST.get () in assert (fmont_as_nat h5 tmp1 == // x15 S.fmul (SI.fsquare_times (fmont_as_nat h4 tmp2) 3) (fmont_as_nat h2 x30)); fsquare_times x30 tmp1 15ul; fmul x30 x30 tmp1; let h6 = ST.get () in assert (fmont_as_nat h6 x30 == // x30 S.fmul (SI.fsquare_times (fmont_as_nat h5 tmp1) 15) (fmont_as_nat h5 tmp1)) inline_for_extraction noextract val finv_256 (x256 x2 x30 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h x256 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a x256 /\ disjoint x30 x2 /\ disjoint x30 x256 /\ disjoint x2 x256 /\ as_nat h a < S.prime /\ as_nat h x30 < S.prime /\ as_nat h x2 < S.prime) (ensures fun h0 _ h1 -> modifies (loc x256 |+| loc x2) h0 h1 /\ as_nat h1 x256 < S.prime /\ (let f = fmont_as_nat h0 a in let x30 = fmont_as_nat h0 x30 in let x2 = fmont_as_nat h0 x2 in let x32_s = S.fmul (SI.fsquare_times x30 2) x2 in let x64_s = S.fmul (SI.fsquare_times x32_s 32) f in let x192_s = S.fmul (SI.fsquare_times x64_s 128) x32_s in let x224_s = S.fmul (SI.fsquare_times x192_s 32) x32_s in let x254_s = S.fmul (SI.fsquare_times x224_s 30) x30 in let x256_s = S.fmul (SI.fsquare_times x254_s 2) f in fmont_as_nat h1 x256 = x256_s))
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
x256: Hacl.Impl.P256.Bignum.felem -> x2: Hacl.Impl.P256.Bignum.felem -> x30: Hacl.Impl.P256.Bignum.felem -> a: Hacl.Impl.P256.Bignum.felem -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Impl.P256.Bignum.felem", "Prims._assert", "Prims.eq2", "Spec.P256.PointOps.felem", "Hacl.Impl.P256.Field.fmont_as_nat", "Spec.P256.PointOps.fmul", "Hacl.Spec.P256.Finv.fsquare_times", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "Hacl.Impl.P256.Field.fmul", "Hacl.Impl.P256.Finv.fsquare_times_in_place", "FStar.UInt32.__uint_to_t", "Hacl.Impl.P256.Finv.fsquare_times" ]
[]
false
true
false
false
false
let finv_256 x256 x2 x30 a =
let h0 = ST.get () in fsquare_times x256 x30 2ul; fmul x256 x256 x2; let h1 = ST.get () in assert (fmont_as_nat h1 x256 == S.fmul (SI.fsquare_times (fmont_as_nat h0 x30) 2) (fmont_as_nat h0 x2)); fsquare_times x2 x256 32ul; fmul x2 x2 a; let h2 = ST.get () in assert (fmont_as_nat h2 x2 == S.fmul (SI.fsquare_times (fmont_as_nat h1 x256) 32) (fmont_as_nat h0 a)); fsquare_times_in_place x2 128ul; fmul x2 x2 x256; let h3 = ST.get () in assert (fmont_as_nat h3 x2 == S.fmul (SI.fsquare_times (fmont_as_nat h2 x2) 128) (fmont_as_nat h1 x256)); fsquare_times_in_place x2 32ul; fmul x2 x2 x256; let h4 = ST.get () in assert (fmont_as_nat h4 x2 == S.fmul (SI.fsquare_times (fmont_as_nat h3 x2) 32) (fmont_as_nat h1 x256)); fsquare_times_in_place x2 30ul; fmul x2 x2 x30; let h5 = ST.get () in assert (fmont_as_nat h5 x2 == S.fmul (SI.fsquare_times (fmont_as_nat h4 x2) 30) (fmont_as_nat h0 x30)); fsquare_times_in_place x2 2ul; fmul x256 x2 a; let h6 = ST.get () in assert (fmont_as_nat h6 x256 == S.fmul (SI.fsquare_times (fmont_as_nat h5 x2) 2) (fmont_as_nat h0 a) )
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.mul_mod
val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid
val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid
let mul_mod ctx x y xy = fmul xy x y
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 36, "end_line": 54, "start_col": 0, "start_line": 54 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let one_mod ctx one = make_fone one inline_for_extraction noextract
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
Hacl.Impl.Exponentiation.Definitions.lmul_st Lib.IntTypes.U64 4ul 0ul Hacl.Impl.P256.Finv.mk_to_p256_prime_comm_monoid
Prims.Tot
[ "total" ]
[]
[ "Lib.Buffer.lbuffer", "Lib.IntTypes.uint_t", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "FStar.UInt32.__uint_to_t", "Hacl.Impl.P256.Field.fmul", "Prims.unit" ]
[]
false
false
false
false
false
let mul_mod ctx x y xy =
fmul xy x y
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.elim_pure
val elim_pure (#uses:_) (p:prop) : SteelGhost unit uses (pure p) (fun _ -> emp) (requires fun _ -> True) (ensures fun _ _ _ -> p)
val elim_pure (#uses:_) (p:prop) : SteelGhost unit uses (pure p) (fun _ -> emp) (requires fun _ -> True) (ensures fun _ _ _ -> p)
let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ())
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 64, "end_line": 606, "start_col": 0, "start_line": 604 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Prims.prop -> Steel.Effect.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Prims.prop", "Steel.Effect.Atomic.rewrite_slprop", "Steel.Effect.Common.to_vprop", "Steel.Memory.emp", "Steel.Effect.Common.emp", "Steel.Memory.mem", "Steel.Effect.Common.reveal_emp", "Prims.unit", "Steel.Effect.Atomic.elim_pure_aux" ]
[]
false
true
false
false
false
let elim_pure #uses p =
let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ())
false
LowParseWriters.fsti
LowParseWriters.read_subcomp_cond
val read_subcomp_cond (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l l': memory_invariant) : GTot Type0
val read_subcomp_cond (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l l': memory_invariant) : GTot Type0
let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err'
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 65, "end_line": 259, "start_col": 0, "start_line": 251 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ())
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> pre: Prims.pure_pre -> post: LowParseWriters.pure_post' a pre -> post_err: LowParseWriters.pure_post_err pre -> pre': Prims.pure_pre -> post': LowParseWriters.pure_post' a pre' -> post_err': LowParseWriters.pure_post_err pre' -> l: LowParseWriters.memory_invariant -> l': LowParseWriters.memory_invariant -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "LowParseWriters.memory_invariant", "Prims.l_and", "LowParseWriters.memory_invariant_includes", "LowParseWriters.read_subcomp_spec_cond" ]
[]
false
false
false
false
true
let read_subcomp_cond (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l l': memory_invariant) : GTot Type0 =
l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err'
false
LowParseWriters.fsti
LowParseWriters.read_subcomp_spec_cond
val read_subcomp_spec_cond (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0
val read_subcomp_spec_cond (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0
let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ())
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 42, "end_line": 248, "start_col": 0, "start_line": 241 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> pre: Prims.pure_pre -> post: LowParseWriters.pure_post' a pre -> post_err: LowParseWriters.pure_post_err pre -> pre': Prims.pure_pre -> post': LowParseWriters.pure_post' a pre' -> post_err': LowParseWriters.pure_post_err pre' -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "Prims.l_and", "Prims.l_imp", "Prims.l_Forall" ]
[]
false
false
false
false
true
let read_subcomp_spec_cond (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 =
(pre' ==> pre) /\ (forall x. (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ())
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.reveal_star_3
val reveal_star_3 (#opened:inames) (p1 p2 p3:vprop) : SteelGhost unit opened (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) )
val reveal_star_3 (#opened:inames) (p1 p2 p3:vprop) : SteelGhost unit opened (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) )
let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3)
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 74, "end_line": 596, "start_col": 0, "start_line": 596 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p1: Steel.Effect.Common.vprop -> p2: Steel.Effect.Common.vprop -> p3: Steel.Effect.Common.vprop -> Steel.Effect.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Atomic.reveal_star_30", "Prims.unit", "Steel.Effect.Common.star", "Steel.Effect.Common.rmem", "Prims.l_True", "Prims.l_and", "Steel.Effect.Common.can_be_split", "Prims.eq2", "Steel.Effect.Common.normal", "Steel.Effect.Common.t_of", "FStar.Pervasives.Native.tuple2", "Steel.Effect.Common.vprop'", "Steel.Effect.Common.__proj__Mkvprop'__item__t", "FStar.Pervasives.Native.Mktuple2" ]
[]
false
true
false
false
false
let reveal_star_3 p1 p2 p3 =
SteelGhost?.reflect (reveal_star_30 p1 p2 p3)
false
LowParseWriters.fsti
LowParseWriters.destr_read_repr_impl
val destr_read_repr_impl (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: (unit -> ERead a pre post post_err l)) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r))
val destr_read_repr_impl (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: (unit -> ERead a pre post post_err l)) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r))
let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ()))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 31, "end_line": 386, "start_col": 0, "start_line": 378 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ()))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> pre: Prims.pure_pre -> post: LowParseWriters.pure_post' a pre -> post_err: LowParseWriters.pure_post_err pre -> l: LowParseWriters.memory_invariant -> $r: (_: Prims.unit -> LowParseWriters.ERead a) -> LowParseWriters.read_repr_impl a pre post post_err l (LowParseWriters.destr_read_repr_spec a pre post post_err l r)
Prims.Tot
[ "total" ]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.__proj__ReadRepr__item__impl", "LowParseWriters.read_repr_impl", "LowParseWriters.destr_read_repr_spec" ]
[]
false
false
false
false
false
let destr_read_repr_impl (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: (unit -> ERead a pre post post_err l)) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) =
ReadRepr?.impl (reify (r ()))
false
LowParseWriters.fsti
LowParseWriters.is_uvar
val is_uvar (t: term) : Tac bool
val is_uvar (t: term) : Tac bool
let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 14, "end_line": 414, "start_col": 0, "start_line": 409 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac Prims.bool
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Tactics.NamedView.term", "Prims.nat", "FStar.Stubs.Reflection.Types.ctx_uvar_and_subst", "Prims.bool", "FStar.Stubs.Reflection.V2.Data.argv", "Prims.list", "FStar.Tactics.NamedView.uu___is_Tv_Uvar", "FStar.Tactics.NamedView.named_term_view", "FStar.Tactics.NamedView.inspect", "FStar.Pervasives.Native.tuple2", "FStar.Tactics.V2.SyntaxHelpers.collect_app" ]
[]
false
true
false
false
false
let is_uvar (t: term) : Tac bool =
match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false
false
Hacl.P256.PrecompTable.fst
Hacl.P256.PrecompTable.lemma_proj_g_pow2_192_eval
val lemma_proj_g_pow2_192_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_128 64 == proj_g_pow2_192)
val lemma_proj_g_pow2_192_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_128 64 == proj_g_pow2_192)
let lemma_proj_g_pow2_192_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_128 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_128 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_128 64); let rX : S.felem = 0xc762a9c8ae1b2f7434ff8da70fe105e0d4f188594989f193de0dbdbf5f60cb9a in let rY : S.felem = 0x1eddaf51836859e1369f1ae8d9ab02e4123b6f151d9b796e297a38fa5613d9bc in let rZ : S.felem = 0xcb433ab3f67815707e398dc7910cc4ec6ea115360060fc73c35b53dce02e2c72 in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ)
{ "file_name": "code/ecdsap256/Hacl.P256.PrecompTable.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 48, "end_line": 102, "start_col": 0, "start_line": 95 }
module Hacl.P256.PrecompTable open FStar.HyperStack open FStar.HyperStack.ST open FStar.Mul open Lib.IntTypes open Lib.Buffer module ST = FStar.HyperStack.ST module LSeq = Lib.Sequence module LE = Lib.Exponentiation module SE = Spec.Exponentiation module SPT = Hacl.Spec.PrecompBaseTable module SPT256 = Hacl.Spec.PrecompBaseTable256 module SPTK = Hacl.Spec.P256.PrecompTable module S = Spec.P256 module SL = Spec.P256.Lemmas open Hacl.Impl.P256.Point include Hacl.Impl.P256.Group #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" let proj_point_to_list p = SPTK.proj_point_to_list_lemma p; SPTK.proj_point_to_list p let lemma_refl x = SPTK.proj_point_to_list_lemma x //----------------- inline_for_extraction noextract let proj_g_pow2_64 : S.proj_point = [@inline_let] let rX : S.felem = 0x000931f4ae428a4ad81ee0aa89cf5247ce85d4dd696c61b4bb9d4761e57b7fbe in [@inline_let] let rY : S.felem = 0x7e88e5e6a142d5c2269f21a158e82ab2c79fcecb26e397b96fd5b9fbcd0a69a5 in [@inline_let] let rZ : S.felem = 0x02626dc2dd5e06cd19de5e6afb6c5dbdd3e41dc1472e7b8ef11eb0662e41c44b in (rX, rY, rZ) val lemma_proj_g_pow2_64_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops S.base_point 64 == proj_g_pow2_64) let lemma_proj_g_pow2_64_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_p256_concrete_ops S.base_point 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops S.base_point 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_p256_concrete_ops S.base_point 64); let rX : S.felem = 0x000931f4ae428a4ad81ee0aa89cf5247ce85d4dd696c61b4bb9d4761e57b7fbe in let rY : S.felem = 0x7e88e5e6a142d5c2269f21a158e82ab2c79fcecb26e397b96fd5b9fbcd0a69a5 in let rZ : S.felem = 0x02626dc2dd5e06cd19de5e6afb6c5dbdd3e41dc1472e7b8ef11eb0662e41c44b in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) inline_for_extraction noextract let proj_g_pow2_128 : S.proj_point = [@inline_let] let rX : S.felem = 0x04c3aaf6c6c00704e96eda89461d63fd2c97ee1e6786fc785e6afac7aa92f9b1 in [@inline_let] let rY : S.felem = 0x14f1edaeb8e9c8d4797d164a3946c7ff50a7c8cd59139a4dbce354e6e4df09c3 in [@inline_let] let rZ : S.felem = 0x80119ced9a5ce83c4e31f8de1a38f89d5f9ff9f637dca86d116a4217f83e55d2 in (rX, rY, rZ) val lemma_proj_g_pow2_128_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_64 64 == proj_g_pow2_128) let lemma_proj_g_pow2_128_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_64 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_64 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_64 64); let rX : S.felem = 0x04c3aaf6c6c00704e96eda89461d63fd2c97ee1e6786fc785e6afac7aa92f9b1 in let rY : S.felem = 0x14f1edaeb8e9c8d4797d164a3946c7ff50a7c8cd59139a4dbce354e6e4df09c3 in let rZ : S.felem = 0x80119ced9a5ce83c4e31f8de1a38f89d5f9ff9f637dca86d116a4217f83e55d2 in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) inline_for_extraction noextract let proj_g_pow2_192 : S.proj_point = [@inline_let] let rX : S.felem = 0xc762a9c8ae1b2f7434ff8da70fe105e0d4f188594989f193de0dbdbf5f60cb9a in [@inline_let] let rY : S.felem = 0x1eddaf51836859e1369f1ae8d9ab02e4123b6f151d9b796e297a38fa5613d9bc in [@inline_let] let rZ : S.felem = 0xcb433ab3f67815707e398dc7910cc4ec6ea115360060fc73c35b53dce02e2c72 in (rX, rY, rZ) val lemma_proj_g_pow2_192_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_128 64 == proj_g_pow2_192)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Exponentiation.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.PrecompBaseTable256.fsti.checked", "Hacl.Spec.PrecompBaseTable.fsti.checked", "Hacl.Spec.P256.PrecompTable.fsti.checked", "Hacl.Impl.P256.Point.fsti.checked", "Hacl.Impl.P256.Group.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.P256.PrecompTable.fst" }
[ { "abbrev": false, "full_module": "Hacl.Impl.P256.Group", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Point", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256.Lemmas", "short_module": "SL" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.PrecompTable", "short_module": "SPTK" }, { "abbrev": true, "full_module": "Hacl.Spec.PrecompBaseTable256", "short_module": "SPT256" }, { "abbrev": true, "full_module": "Hacl.Spec.PrecompBaseTable", "short_module": "SPT" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": true, "full_module": "Lib.Exponentiation", "short_module": "LE" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Group", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Point", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Hacl.Spec.PrecompBaseTable", "short_module": "SPT" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation.Definitions", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": true, "full_module": "Lib.Exponentiation.Definition", "short_module": "LE" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
_: Prims.unit -> FStar.Pervasives.Lemma (ensures Spec.Exponentiation.exp_pow2 Spec.P256.mk_p256_concrete_ops Hacl.P256.PrecompTable.proj_g_pow2_128 64 == Hacl.P256.PrecompTable.proj_g_pow2_192)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.unit", "Prims.nat", "FStar.Pervasives.assert_norm", "Prims.l_and", "Prims.eq2", "Spec.P256.PointOps.felem", "FStar.Pervasives.normalize_term_spec", "Spec.P256.PointOps.proj_point", "Hacl.Spec.PrecompBaseTable256.exp_pow2_rec", "Spec.P256.mk_p256_concrete_ops", "Hacl.P256.PrecompTable.proj_g_pow2_128", "FStar.Pervasives.Native.tuple3", "FStar.Pervasives.normalize_term", "Hacl.Spec.PrecompBaseTable256.exp_pow2_rec_is_exp_pow2" ]
[]
false
false
true
false
false
let lemma_proj_g_pow2_192_eval () =
SPT256.exp_pow2_rec_is_exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_128 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_128 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_128 64); let rX:S.felem = 0xc762a9c8ae1b2f7434ff8da70fe105e0d4f188594989f193de0dbdbf5f60cb9a in let rY:S.felem = 0x1eddaf51836859e1369f1ae8d9ab02e4123b6f151d9b796e297a38fa5613d9bc in let rZ:S.felem = 0xcb433ab3f67815707e398dc7910cc4ec6ea115360060fc73c35b53dce02e2c72 in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ)
false
LowParseWriters.fsti
LowParseWriters.destr_read_repr_spec
val destr_read_repr_spec (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: (unit -> ERead a pre post post_err l)) : Tot (read_repr_spec a pre post post_err)
val destr_read_repr_spec (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: (unit -> ERead a pre post post_err l)) : Tot (read_repr_spec a pre post post_err)
let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ()))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 31, "end_line": 375, "start_col": 0, "start_line": 367 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> pre: Prims.pure_pre -> post: LowParseWriters.pure_post' a pre -> post_err: LowParseWriters.pure_post_err pre -> l: LowParseWriters.memory_invariant -> $r: (_: Prims.unit -> LowParseWriters.ERead a) -> LowParseWriters.read_repr_spec a pre post post_err
Prims.Tot
[ "total" ]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.__proj__ReadRepr__item__spec", "LowParseWriters.read_repr_spec" ]
[]
false
false
false
false
false
let destr_read_repr_spec (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: (unit -> ERead a pre post post_err l)) : Tot (read_repr_spec a pre post post_err) =
ReadRepr?.spec (reify (r ()))
false
LowParseWriters.fsti
LowParseWriters.lift_pure_read
val lift_pure_read (a: Type) (wp: pure_wp a) (l: memory_invariant) (f_pure: (unit -> PURE a wp)) : Tot (read_repr a (wp (fun _ -> True)) (fun x -> ~(wp (fun x' -> ~(x == x')))) (fun _ -> False) l)
val lift_pure_read (a: Type) (wp: pure_wp a) (l: memory_invariant) (f_pure: (unit -> PURE a wp)) : Tot (read_repr a (wp (fun _ -> True)) (fun x -> ~(wp (fun x' -> ~(x == x')))) (fun _ -> False) l)
let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 48, "end_line": 363, "start_col": 0, "start_line": 353 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> wp: Prims.pure_wp a -> l: LowParseWriters.memory_invariant -> f_pure: (_: Prims.unit -> Prims.PURE a) -> LowParseWriters.read_repr a (wp (fun _ -> Prims.l_True)) (fun x -> ~(wp (fun x' -> ~(x == x')))) (fun _ -> Prims.l_False) l
Prims.Tot
[ "total" ]
[]
[ "Prims.pure_wp", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.ReadRepr", "Prims.l_True", "Prims.l_not", "Prims.eq2", "Prims.l_False", "LowParseWriters.lift_pure_read_spec", "LowParseWriters.lift_pure_read_impl", "FStar.Monotonic.Pure.elim_pure_wp_monotonicity_forall", "LowParseWriters.read_repr" ]
[]
false
false
false
false
false
let lift_pure_read (a: Type) (wp: pure_wp a) (l: memory_invariant) (f_pure: (unit -> PURE a wp)) : Tot (read_repr a (wp (fun _ -> True)) (fun x -> ~(wp (fun x' -> ~(x == x')))) (fun _ -> False) l) =
elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l)
false
LowParseWriters.fsti
LowParseWriters.rewrite_eqs_from_context_non_sq
val rewrite_eqs_from_context_non_sq: Prims.unit -> Tac unit
val rewrite_eqs_from_context_non_sq: Prims.unit -> Tac unit
let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ()))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 81, "end_line": 464, "start_col": 0, "start_line": 463 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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 -> FStar.Tactics.Effect.Tac Prims.unit
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.unit", "LowParseWriters.rewrite_all_context_equalities", "FStar.Tactics.NamedView.binders", "FStar.List.Tot.Base.map", "FStar.Tactics.NamedView.binding", "FStar.Tactics.NamedView.binder", "FStar.Tactics.NamedView.binding_to_binder", "Prims.list", "FStar.Tactics.V2.Derived.cur_vars" ]
[]
false
true
false
false
false
let rewrite_eqs_from_context_non_sq () : Tac unit =
rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ()))
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.refl
val refl (a: LSeq.lseq uint64 4 {linv a}) : GTot S.felem
val refl (a: LSeq.lseq uint64 4 {linv a}) : GTot S.felem
let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a)
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 26, "end_line": 34, "start_col": 0, "start_line": 33 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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: Lib.Sequence.lseq Lib.IntTypes.uint64 4 {Hacl.Impl.P256.Finv.linv a} -> Prims.GTot Spec.P256.PointOps.felem
Prims.GTot
[ "sometrivial" ]
[]
[ "Lib.Sequence.lseq", "Lib.IntTypes.uint64", "Hacl.Impl.P256.Finv.linv", "Hacl.Spec.P256.Montgomery.from_mont", "Hacl.Spec.Bignum.Definitions.bn_v", "Lib.IntTypes.U64", "Spec.P256.PointOps.felem" ]
[]
false
false
false
false
false
let refl (a: LSeq.lseq uint64 4 {linv a}) : GTot S.felem =
SM.from_mont (BD.bn_v a)
false
LowParseWriters.fsti
LowParseWriters.is_eq
val is_eq (t: term) : Tac (option (term & term))
val is_eq (t: term) : Tac (option (term & term))
let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 13, "end_line": 419, "start_col": 0, "start_line": 416 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option (FStar.Tactics.NamedView.term * FStar.Tactics.NamedView.term))
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Tactics.NamedView.term", "FStar.Pervasives.Native.option", "FStar.Stubs.Reflection.Types.typ", "FStar.Pervasives.Native.Some", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "FStar.Reflection.V2.Formula.formula", "FStar.Pervasives.Native.None", "FStar.Reflection.V2.Formula.term_as_formula" ]
[]
false
true
false
false
false
let is_eq (t: term) : Tac (option (term & term)) =
match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None
false
LowParseWriters.fsti
LowParseWriters.rewrite_eqs_from_context
val rewrite_eqs_from_context: Prims.unit -> Tac unit
val rewrite_eqs_from_context: Prims.unit -> Tac unit
let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq ()
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 36, "end_line": 468, "start_col": 0, "start_line": 466 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ()))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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 -> FStar.Tactics.Effect.Tac Prims.unit
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.unit", "LowParseWriters.rewrite_eqs_from_context_non_sq", "FStar.Tactics.V2.Derived.rewrite_eqs_from_context" ]
[]
false
true
false
false
false
let rewrite_eqs_from_context () : Tac unit =
rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq ()
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.elim_vrewrite
val elim_vrewrite (#opened:inames) (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : SteelGhost unit opened (vrewrite v f) (fun _ -> v) (fun _ -> True) (fun h _ h' -> h (vrewrite v f) == f (h' v))
val elim_vrewrite (#opened:inames) (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : SteelGhost unit opened (vrewrite v f) (fun _ -> v) (fun _ -> True) (fun h _ h' -> h (vrewrite v f) == f (h' v))
let elim_vrewrite v #t f = change_slprop_rel (vrewrite v f) v (fun y x -> y == f x) (fun m -> vrewrite_sel_eq v f m)
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 36, "end_line": 862, "start_col": 0, "start_line": 855 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i (* * AR: SteelAtomic and SteelGhost are not marked reifiable since we intend to run Steel programs natively * However to implement the with_inv combinators we need to reify their thunks to reprs * We could implement it better by having support for reification only in the .fst file * But for now assuming a function *) assume val reify_steel_atomic_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#g:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelAtomicBase a framed opened_invariants g pre post req ens) : repr a framed opened_invariants g pre post req ens [@@warn_on_use "as_unobservable_atomic_action is a trusted primitive"] let as_atomic_o_action (#a:Type u#a) (#opened_invariants:inames) (#fp:slprop) (#fp': a -> slprop) (o:observability) (f:action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) = SteelAtomicBaseT?.reflect f let with_invariant #a #fp #fp' #obs #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_o_action obs (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_atomic_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return x assume val reify_steel_ghost_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelGhostBase a framed opened_invariants Unobservable pre post req ens) : repr a framed opened_invariants Unobservable pre post req ens let with_invariant_g #a #fp #fp' #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_unobservable_action (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_ghost_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return (hide x) let intro_vrefine v p = let m = get () in let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased (vrefine_t v p) = Ghost.hide (Ghost.reveal x) in change_slprop v (vrefine v p) x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let elim_vrefine v p = let h = get() in let x : Ghost.erased (vrefine_t v p) = gget (vrefine v p) in let x' : Ghost.erased (t_of v) = Ghost.hide (Ghost.reveal x) in change_slprop (vrefine v p) v x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let vdep_cond (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) : Tot prop = q == p (fst x1) let vdep_rel (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) (x2: (t_of (vdep v p))) : Tot prop = q == p (fst x1) /\ dfst (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == fst x1 /\ dsnd (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == snd x1 let intro_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (v `star` q)) m /\ q == p (fst (sel_of (v `star` q) m)) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let intro_vdep v q p = reveal_star v q; change_slprop_rel_with_cond (v `star` q) (vdep v p) (vdep_cond v q p) (vdep_rel v q p) (fun m -> intro_vdep_lemma v q p m) let vdep_cond_recip (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) (x2: t_of (vdep v p)) : Tot prop = q == p (dfst (x2 <: dtuple2 (t_of v) (vdep_payload v p))) let vdep_rel_recip (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x2: (t_of (vdep v p))) (x1: t_of (v `star` q)) : Tot prop = vdep_rel v q p x1 x2 let elim_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (vdep v p)) m /\ q == p (dfst (sel_of (vdep v p) m <: dtuple2 (t_of v) (vdep_payload v p))) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let elim_vdep0 (#opened:inames) (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) : SteelGhost unit opened (vdep v p) (fun _ -> v `star` q) (requires (fun h -> q == p (dfst (h (vdep v p))))) (ensures (fun h _ h' -> let fs = h' v in let sn = h' q in let x2 = h (vdep v p) in q == p fs /\ dfst x2 == fs /\ dsnd x2 == sn )) = change_slprop_rel_with_cond (vdep v p) (v `star` q) (vdep_cond_recip v p q) (vdep_rel_recip v q p) (fun m -> elim_vdep_lemma v q p m); reveal_star v q let elim_vdep v p = let r = gget (vdep v p) in let res = Ghost.hide (dfst #(t_of v) #(vdep_payload v p) (Ghost.reveal r)) in elim_vdep0 v p (p (Ghost.reveal res)); res let intro_vrewrite v #t f = let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased t = Ghost.hide (f (Ghost.reveal x)) in change_slprop v (vrewrite v f) x x' (fun m -> vrewrite_sel_eq v f m )
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
v: Steel.Effect.Common.vprop -> f: (_: Steel.Effect.Common.normal (Steel.Effect.Common.t_of v) -> Prims.GTot t) -> Steel.Effect.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Common.normal", "Steel.Effect.Common.t_of", "Steel.Effect.Atomic.change_slprop_rel", "Steel.Effect.Common.vrewrite", "Prims.eq2", "Prims.prop", "Steel.Memory.mem", "Steel.Effect.Common.vrewrite_sel_eq", "Prims.unit" ]
[]
false
true
false
false
false
let elim_vrewrite v #t f =
change_slprop_rel (vrewrite v f) v (fun y x -> y == f x) (fun m -> vrewrite_sel_eq v f m)
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.permutation_from_equal_counts
val permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1)
val permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1)
let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f)
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 8, "end_line": 238, "start_col": 0, "start_line": 127 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 1, "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": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s0: FStar.Seq.Base.seq a -> s1: FStar.Seq.Base.seq a {forall (x: a). FStar.Seq.Properties.count x s0 == FStar.Seq.Properties.count x s1} -> Prims.Tot (FStar.Seq.Permutation.seqperm s0 s1)
Prims.Tot
[ "", "total" ]
[]
[ "Prims.eqtype", "FStar.Seq.Base.seq", "Prims.l_Forall", "Prims.eq2", "Prims.nat", "FStar.Seq.Properties.count", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "Prims.unit", "FStar.Seq.Permutation.equal_counts_empty", "FStar.Seq.Permutation.reveal_is_permutation_pats", "FStar.Seq.Permutation.index_fun", "FStar.IntegerIntervals.under", "Prims.bool", "FStar.Seq.Permutation.introduce_is_permutation", "Prims.squash", "Prims.b2t", "Prims.op_LessThan", "FStar.Seq.Base.index", "FStar.Classical.Sugar.forall_intro", "Prims.op_Subtraction", "Prims._assert", "FStar.Seq.Properties.tail", "Prims.l_or", "FStar.IntegerIntervals.interval_condition", "Prims.op_Addition", "Prims.l_imp", "Prims.op_disEquality", "FStar.Classical.Sugar.implies_intro", "Prims.op_BarBar", "Prims.op_GreaterThanOrEqual", "FStar.Seq.Permutation.adapt_index_fun", "FStar.Seq.Permutation.seqperm", "FStar.Seq.Permutation.permutation_from_equal_counts", "FStar.Seq.Base.append", "FStar.Seq.Properties.head", "FStar.Calc.calc_finish", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "FStar.Calc.calc_step", "FStar.Seq.Base.create", "FStar.Seq.Base.cons", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Seq.Permutation.count_singleton_one", "FStar.Seq.Properties.lemma_append_count_aux", "FStar.Seq.Base.equal", "FStar.Seq.Permutation.count_singleton_zero", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.fst", "FStar.Pervasives.Native.snd", "FStar.Seq.Permutation.find", "Prims.op_GreaterThan", "FStar.Seq.Permutation.count_head" ]
[ "recursion" ]
false
false
false
false
false
let rec permutation_from_equal_counts (#a: eqtype) (s0: seq a) (s1: seq a {(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) =
if Seq.length s0 = 0 then (let f:index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f) else (count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x . count x (Seq.tail s0) == count x (Seq.append pfx sfx) with (if x = Seq.head s0 then (calc (eq2 #int) { count x (Seq.tail s0) <: int; ( == ) { (assert (s0 `Seq.equal` (Seq.cons (Seq.head s0) (Seq.tail s0))); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x) } count x s0 - 1 <: int; ( == ) { () } count x s1 - 1 <: int; ( == ) { () } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; ( == ) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; ( == ) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; ( == ) { count_singleton_one x } count x pfx + count x sfx <: int; ( == ) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; }) else (calc ( == ) { count x (Seq.tail s0); ( == ) { (assert (s0 `Seq.equal` (Seq.cons (Seq.head s0) (Seq.tail s0))); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0)) } count x s0; ( == ) { () } count x s1; ( == ) { () } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); ( == ) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); ( == ) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx); ( == ) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; ( == ) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); })); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f:index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq:squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y . x <> y ==> f x <> f y with (introduce _ ==> _ with _. (if f x = n || f y = n then () else if f' (x - 1) < n then (assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1)) else (assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1)))) in let perm_prop = introduce forall (i: nat{i < Seq.length s0}) . Seq.index s0 i == Seq.index s1 (f i) with (if i = 0 then () else if f' (i - 1) < n then (assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1)))) else (assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))))) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f)
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.mk_selector_vprop_intro
val mk_selector_vprop_intro (#opened: _) (#t: Type0) (#x: t) (p: t -> vprop) (p_inj: interp_hp_of_injective p) : SteelGhost unit opened (p x) (fun _ -> mk_selector_vprop p p_inj) (fun _ -> True) (fun _ _ h' -> h' (mk_selector_vprop p p_inj) == x)
val mk_selector_vprop_intro (#opened: _) (#t: Type0) (#x: t) (p: t -> vprop) (p_inj: interp_hp_of_injective p) : SteelGhost unit opened (p x) (fun _ -> mk_selector_vprop p p_inj) (fun _ -> True) (fun _ _ h' -> h' (mk_selector_vprop p p_inj) == x)
let mk_selector_vprop_intro #_ #_ #x p p_inj = change_slprop_rel (p _) (mk_selector_vprop p p_inj) (fun _ x' -> x == x') (fun m -> intro_h_exists x (hp_of_pointwise p) m; let x' = mk_selector_vprop_sel' p p_inj m in p_inj x x' m )
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 5, "end_line": 924, "start_col": 0, "start_line": 914 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i (* * AR: SteelAtomic and SteelGhost are not marked reifiable since we intend to run Steel programs natively * However to implement the with_inv combinators we need to reify their thunks to reprs * We could implement it better by having support for reification only in the .fst file * But for now assuming a function *) assume val reify_steel_atomic_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#g:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelAtomicBase a framed opened_invariants g pre post req ens) : repr a framed opened_invariants g pre post req ens [@@warn_on_use "as_unobservable_atomic_action is a trusted primitive"] let as_atomic_o_action (#a:Type u#a) (#opened_invariants:inames) (#fp:slprop) (#fp': a -> slprop) (o:observability) (f:action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) = SteelAtomicBaseT?.reflect f let with_invariant #a #fp #fp' #obs #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_o_action obs (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_atomic_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return x assume val reify_steel_ghost_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelGhostBase a framed opened_invariants Unobservable pre post req ens) : repr a framed opened_invariants Unobservable pre post req ens let with_invariant_g #a #fp #fp' #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_unobservable_action (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_ghost_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return (hide x) let intro_vrefine v p = let m = get () in let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased (vrefine_t v p) = Ghost.hide (Ghost.reveal x) in change_slprop v (vrefine v p) x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let elim_vrefine v p = let h = get() in let x : Ghost.erased (vrefine_t v p) = gget (vrefine v p) in let x' : Ghost.erased (t_of v) = Ghost.hide (Ghost.reveal x) in change_slprop (vrefine v p) v x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let vdep_cond (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) : Tot prop = q == p (fst x1) let vdep_rel (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) (x2: (t_of (vdep v p))) : Tot prop = q == p (fst x1) /\ dfst (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == fst x1 /\ dsnd (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == snd x1 let intro_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (v `star` q)) m /\ q == p (fst (sel_of (v `star` q) m)) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let intro_vdep v q p = reveal_star v q; change_slprop_rel_with_cond (v `star` q) (vdep v p) (vdep_cond v q p) (vdep_rel v q p) (fun m -> intro_vdep_lemma v q p m) let vdep_cond_recip (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) (x2: t_of (vdep v p)) : Tot prop = q == p (dfst (x2 <: dtuple2 (t_of v) (vdep_payload v p))) let vdep_rel_recip (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x2: (t_of (vdep v p))) (x1: t_of (v `star` q)) : Tot prop = vdep_rel v q p x1 x2 let elim_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (vdep v p)) m /\ q == p (dfst (sel_of (vdep v p) m <: dtuple2 (t_of v) (vdep_payload v p))) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let elim_vdep0 (#opened:inames) (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) : SteelGhost unit opened (vdep v p) (fun _ -> v `star` q) (requires (fun h -> q == p (dfst (h (vdep v p))))) (ensures (fun h _ h' -> let fs = h' v in let sn = h' q in let x2 = h (vdep v p) in q == p fs /\ dfst x2 == fs /\ dsnd x2 == sn )) = change_slprop_rel_with_cond (vdep v p) (v `star` q) (vdep_cond_recip v p q) (vdep_rel_recip v q p) (fun m -> elim_vdep_lemma v q p m); reveal_star v q let elim_vdep v p = let r = gget (vdep v p) in let res = Ghost.hide (dfst #(t_of v) #(vdep_payload v p) (Ghost.reveal r)) in elim_vdep0 v p (p (Ghost.reveal res)); res let intro_vrewrite v #t f = let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased t = Ghost.hide (f (Ghost.reveal x)) in change_slprop v (vrewrite v f) x x' (fun m -> vrewrite_sel_eq v f m ) let elim_vrewrite v #t f = change_slprop_rel (vrewrite v f) v (fun y x -> y == f x) (fun m -> vrewrite_sel_eq v f m) /// Deriving a selector-style vprop from an injective pts-to-style vprop let hp_of_pointwise (#t: Type) (p: t -> vprop) (x: t) : Tot slprop = hp_of (p x) let mk_selector_vprop_hp p = Steel.Memory.h_exists (hp_of_pointwise p) let mk_selector_vprop_sel' (#t: Type) (p: t -> vprop) (p_inj: interp_hp_of_injective p) // unused in the definition, but necessary for the local SMTPats below : Tot (selector' t (mk_selector_vprop_hp p)) = fun m -> id_elim_exists (hp_of_pointwise p) m let mk_selector_vprop_sel #t p p_inj = let varrayp_sel_depends_only_on (#t: Type) (p: t -> vprop) (p_inj: interp_hp_of_injective p) (m0: Steel.Memory.hmem (mk_selector_vprop_hp p)) (m1: mem { disjoint m0 m1 }) : Lemma ( mk_selector_vprop_sel' p p_inj m0 == mk_selector_vprop_sel' p p_inj (Steel.Memory.join m0 m1) ) [SMTPat (mk_selector_vprop_sel' p p_inj (Steel.Memory.join m0 m1))] = p_inj (mk_selector_vprop_sel' p p_inj m0) (mk_selector_vprop_sel' p p_inj (Steel.Memory.join m0 m1)) (Steel.Memory.join m0 m1) in let varrayp_sel_depends_only_on_core (#t: Type) (p: t -> vprop) (p_inj: interp_hp_of_injective p) (m0: Steel.Memory.hmem (mk_selector_vprop_hp p)) : Lemma ( mk_selector_vprop_sel' p p_inj m0 == mk_selector_vprop_sel' p p_inj (core_mem m0) ) [SMTPat (mk_selector_vprop_sel' p p_inj (core_mem m0))] = p_inj (mk_selector_vprop_sel' p p_inj m0) (mk_selector_vprop_sel' p p_inj (core_mem m0)) m0 in mk_selector_vprop_sel' p p_inj
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: (_: t -> Steel.Effect.Common.vprop) -> p_inj: Steel.Effect.Atomic.interp_hp_of_injective p -> Steel.Effect.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Atomic.interp_hp_of_injective", "Steel.Effect.Atomic.change_slprop_rel", "Steel.Effect.Atomic.mk_selector_vprop", "Steel.Effect.Common.normal", "Steel.Effect.Common.t_of", "Prims.eq2", "Prims.prop", "Steel.Memory.mem", "Steel.Effect.Atomic.mk_selector_vprop_sel'", "Prims.unit", "Steel.Memory.intro_h_exists", "Steel.Effect.Atomic.hp_of_pointwise" ]
[]
false
true
false
false
false
let mk_selector_vprop_intro #_ #_ #x p p_inj =
change_slprop_rel (p _) (mk_selector_vprop p p_inj) (fun _ x' -> x == x') (fun m -> intro_h_exists x (hp_of_pointwise p) m; let x' = mk_selector_vprop_sel' p p_inj m in p_inj x x' m)
false
LowParseWriters.fsti
LowParseWriters.tac
val tac: Prims.unit -> Tac unit
val tac: Prims.unit -> Tac unit
let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 484, "start_col": 0, "start_line": 471 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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 -> FStar.Tactics.Effect.Tac Prims.unit
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.unit", "Prims.bool", "LowParseWriters.tac", "FStar.Tactics.V2.Derived.or_else", "FStar.Tactics.V2.Derived.trefl", "FStar.Stubs.Tactics.V2.Builtins.norm", "Prims.Nil", "FStar.Pervasives.norm_step", "LowParseWriters.rewrite_eqs_from_context", "FStar.Tactics.V2.Derived.assumption", "LowParseWriters.pick_next", "Prims.nat", "FStar.List.Tot.Base.length", "FStar.Stubs.Tactics.Types.goal", "Prims.list", "FStar.Tactics.V2.Derived.goals", "Prims.op_Equality", "Prims.int" ]
[ "recursion" ]
false
true
false
false
false
let rec tac () : Tac unit =
if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then (or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac ()) else (rewrite_eqs_from_context (); norm []; trefl (); tac ())
false
FStar.Seq.Permutation.fst
FStar.Seq.Permutation.foldm_snoc_split'
val foldm_snoc_split' (#c #eq: _) (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: ((ifrom_ito n0 nk) -> c)) : Tot (squash ((foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk))) `eq.eq` (cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)) )))) (decreases nk - n0)
val foldm_snoc_split' (#c #eq: _) (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: ((ifrom_ito n0 nk) -> c)) : Tot (squash ((foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk))) `eq.eq` (cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)) )))) (decreases nk - n0)
let rec foldm_snoc_split' #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Tot (squash (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk))))) (decreases nk-n0) = if (nk=n0) then fold_decomposition_aux cm n0 nk expr1 expr2 else ( cm_commutativity cm; elim_eq_laws eq; let lfunc_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr (func_sum cm expr1 expr2) n0 nf in let range_count = closed_interval_size in let full_count = range_count n0 nk in let sub_count = range_count n0 (nk-1) in let fullseq = init full_count (lfunc_up_to nk) in let rfunc_1_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr1 n0 nf in let rfunc_2_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr2 n0 nf in let fullseq_r1 = init full_count (rfunc_1_up_to nk) in let fullseq_r2 = init full_count (rfunc_2_up_to nk) in let subseq = init sub_count (lfunc_up_to nk) in let subfold = foldm_snoc cm subseq in let last = lfunc_up_to nk sub_count in lemma_eq_elim (fst (un_snoc fullseq)) subseq; // subseq is literally (liat fullseq) let fullfold = foldm_snoc cm fullseq in let subseq_r1 = init sub_count (rfunc_1_up_to nk) in let subseq_r2 = init sub_count (rfunc_2_up_to nk) in lemma_eq_elim (fst (un_snoc fullseq_r1)) subseq_r1; // subseq is literally (liat fullseq) lemma_eq_elim (fst (un_snoc fullseq_r2)) subseq_r2; // subseq is literally (liat fullseq) lemma_eq_elim (init sub_count (lfunc_up_to nk)) subseq; lemma_eq_elim (init sub_count (lfunc_up_to (nk-1))) subseq; lemma_eq_elim subseq_r1 (init sub_count (rfunc_1_up_to (nk-1))); lemma_eq_elim subseq_r2 (init sub_count (rfunc_2_up_to (nk-1))); let fullfold_r1 = foldm_snoc cm fullseq_r1 in let fullfold_r2 = foldm_snoc cm fullseq_r2 in let subfold_r1 = foldm_snoc cm subseq_r1 in let subfold_r2 = foldm_snoc cm subseq_r2 in cm.congruence (foldm_snoc cm (init sub_count (rfunc_1_up_to (nk-1)))) (foldm_snoc cm (init sub_count (rfunc_2_up_to (nk-1)))) subfold_r1 subfold_r2; let last_r1 = rfunc_1_up_to nk sub_count in let last_r2 = rfunc_2_up_to nk sub_count in let nk' = nk - 1 in (* here's the nasty bit with where we have to massage the proof from the induction hypothesis *) let ih : squash ((foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')) `eq.eq` cm.mult (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))) (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))))) = foldm_snoc_split' cm n0 nk' expr1 expr2 in calc (==) { subfold; == { } foldm_snoc cm subseq; == { assert (Seq.equal subseq (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk'))) } foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')); }; assert (Seq.equal subseq_r1 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))); assert (Seq.equal subseq_r2 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))); let _ : squash (subfold `eq.eq` (subfold_r1 `cm.mult` subfold_r2)) = ih in cm.congruence subfold last (subfold_r1 `cm.mult` subfold_r2) last; aux_shuffle_lemma cm subfold_r1 subfold_r2 (rfunc_1_up_to nk sub_count) (rfunc_2_up_to nk sub_count); cm.congruence (subfold_r1 `cm.mult` (rfunc_1_up_to nk sub_count)) (subfold_r2 `cm.mult` (rfunc_2_up_to nk sub_count)) (foldm_snoc cm fullseq_r1) (foldm_snoc cm fullseq_r2) )
{ "file_name": "ulib/FStar.Seq.Permutation.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 628, "start_col": 0, "start_line": 557 }
(* Copyright 2021-2022 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. Authors: N. Swamy, A. Rastogi, A. Rozanov *) module FStar.Seq.Permutation open FStar.Seq open FStar.Calc [@@"opaque_to_smt"] let is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) = Seq.length s0 == Seq.length s1 /\ (forall x y. // {:pattern f x; f y} x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). // {:pattern (Seq.index s1 (f i))} Seq.index s0 i == Seq.index s1 (f i)) let reveal_is_permutation #a (s0 s1:seq a) (f:index_fun s0) = reveal_opaque (`%is_permutation) (is_permutation s0 s1 f) let reveal_is_permutation_nopats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let split3_index (#a:eqtype) (s0:seq a) (x:a) (s1:seq a) (j:nat) : Lemma (requires j < Seq.length (Seq.append s0 s1)) (ensures ( let s = Seq.append s0 (Seq.cons x s1) in let s' = Seq.append s0 s1 in let n = Seq.length s0 in if j < n then Seq.index s' j == Seq.index s j else Seq.index s' j == Seq.index s (j + 1) )) = let n = Seq.length (Seq.append s0 s1) in if j < n then () else () #push-options "--fuel 2 --ifuel 0 --z3rlimit_factor 2" let rec find (#a:eqtype) (x:a) (s:seq a{ count x s > 0 }) : Tot (frags:(seq a & seq a) { let s' = Seq.append (fst frags) (snd frags) in let n = Seq.length (fst frags) in s `Seq.equal` Seq.append (fst frags) (Seq.cons x (snd frags)) }) (decreases (Seq.length s)) = if Seq.head s = x then Seq.empty, Seq.tail s else ( let pfx, sfx = find x (Seq.tail s) in assert (Seq.equal (Seq.tail s) (Seq.append pfx (Seq.cons x sfx))); assert (Seq.equal s (Seq.cons (Seq.head s) (Seq.tail s))); Seq.cons (Seq.head s) pfx, sfx ) #pop-options let introduce_is_permutation (#a:Type) (s0:seq a) (s1:seq a) (f:index_fun s0) (_:squash (Seq.length s0 == Seq.length s1)) (_:squash (forall x y. x <> y ==> f x <> f y)) (_:squash (forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i))) : Lemma (ensures is_permutation s0 s1 f) = reveal_is_permutation_nopats s0 s1 f let reveal_is_permutation_pats (#a:Type) (s0 s1:seq a) (f:index_fun s0) : Lemma (is_permutation s0 s1 f <==> Seq.length s0 == Seq.length s1 /\ (forall x y. {:pattern f x; f y } x <> y ==> f x <> f y) /\ (forall (i:nat{i < Seq.length s0}). {:pattern (Seq.index s0 i)} Seq.index s0 i == Seq.index s1 (f i))) = reveal_is_permutation s0 s1 f let adapt_index_fun (s:Seq.seq 'a { Seq.length s > 0 }) (f:index_fun (Seq.tail s)) (n:nat{n < Seq.length s}) : index_fun s = fun i -> if i = 0 then n else if f (i - 1) < n then f (i - 1) else f (i - 1) + 1 let count_singleton_one (#a:eqtype) (x:a) : Lemma (count x (Seq.create 1 x) == 1) = () let count_singleton_zero (#a:eqtype) (x y:a) : Lemma (x =!= y ==> count x (Seq.create 1 y) == 0) = () let equal_counts_empty (#a:eqtype) (s0 s1:seq a) : Lemma (requires Seq.length s0 == 0 /\ (forall x. count x s0 == count x s1)) (ensures Seq.length s1 == 0) = if Seq.length s1 > 0 then assert (count (Seq.head s1) s1 > 0) let count_head (#a:eqtype) (x:seq a{ Seq.length x > 0 }) : Lemma (count (Seq.head x) x > 0) = () #push-options "--fuel 0" #restart-solver let rec permutation_from_equal_counts (#a:eqtype) (s0:seq a) (s1:seq a{(forall x. count x s0 == count x s1)}) : Tot (seqperm s0 s1) (decreases (Seq.length s0)) = if Seq.length s0 = 0 then ( let f : index_fun s0 = fun i -> i in reveal_is_permutation_pats s0 s1 f; equal_counts_empty s0 s1; f ) else ( count_head s0; assert (count (Seq.head s0) s0 > 0); let pfx, sfx = find (Seq.head s0) s1 in introduce forall x. count x (Seq.tail s0) == count x (Seq.append pfx sfx) with ( if x = Seq.head s0 then ( calc (eq2 #int) { count x (Seq.tail s0) <: int; (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux (Seq.head s0) (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_one x } count x s0 - 1 <: int; (==) {} count x s1 - 1 <: int; (==) {} count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)) - 1 <: int; (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx) - 1 <: int; (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) - 1 <: int; (==) { count_singleton_one x } count x pfx + count x sfx <: int; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx) <: int; } ) else ( calc (==) { count x (Seq.tail s0); (==) { assert (s0 `Seq.equal` Seq.cons (Seq.head s0) (Seq.tail s0)); lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) (Seq.tail s0); count_singleton_zero x (Seq.head s0) } count x s0; (==) { } count x s1; (==) { } count x (Seq.append pfx (Seq.cons (Seq.head s0) sfx)); (==) { lemma_append_count_aux x pfx (Seq.cons (Seq.head s0) sfx) } count x pfx + count x (Seq.cons (Seq.head s0) sfx); (==) { lemma_append_count_aux x (Seq.create 1 (Seq.head s0)) sfx } count x pfx + (count x (Seq.create 1 (Seq.head s0)) + count x sfx) ; (==) { count_singleton_zero x (Seq.head s0) } count x pfx + count x sfx; (==) { lemma_append_count_aux x pfx sfx } count x (Seq.append pfx sfx); } ) ); let s1' = (Seq.append pfx sfx) in let f' = permutation_from_equal_counts (Seq.tail s0) s1' in reveal_is_permutation_pats (Seq.tail s0) s1' f'; let n = Seq.length pfx in let f : index_fun s0 = adapt_index_fun s0 f' n in assert (Seq.length s0 == Seq.length s1); let len_eq : squash (Seq.length s0 == Seq.length s1) = () in assert (forall x y. x <> y ==> f' x <> f' y); let neq = introduce forall x y. x <> y ==> f x <> f y with (introduce _ ==> _ with _. ( if f x = n || f y = n then () else if f' (x - 1) < n then ( assert (f x == f' (x - 1)); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) else ( assert (f x == f' (x - 1) + 1); if f' (y - 1) < n then assert (f y == f' (y - 1)) else assert (f y == f' (y - 1) + 1) ) ) ) in let perm_prop = introduce forall (i:nat{i < Seq.length s0}). Seq.index s0 i == Seq.index s1 (f i) with ( if i = 0 then () else if f' (i - 1) < n then ( assert (f i == f' (i - 1)); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) else ( assert (f i = f' (i - 1) + 1); assert (Seq.index (Seq.tail s0) (i - 1) == Seq.index s1' (f' (i - 1))) ) ) in introduce_is_permutation s0 s1 f len_eq neq perm_prop; f) #pop-options module CE = FStar.Algebra.CommMonoid.Equiv let elim_monoid_laws #a #eq (m:CE.cm a eq) : Lemma ( (forall x y. {:pattern m.mult x y} eq.eq (m.mult x y) (m.mult y x)) /\ (forall x y z.{:pattern (m.mult x (m.mult y z))} eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z)) /\ (forall x.{:pattern (m.mult x m.unit)} eq.eq (m.mult x m.unit) x) ) = introduce forall x y. eq.eq (m.mult x y) (m.mult y x) with ( m.commutativity x y ); introduce forall x y z. eq.eq (m.mult x (m.mult y z)) (m.mult (m.mult x y) z) with ( m.associativity x y z; eq.symmetry (m.mult (m.mult x y) z) (m.mult x (m.mult y z)) ); introduce forall x. eq.eq (m.mult x m.unit) x with ( CE.right_identity eq m x ) let rec foldm_snoc_unit_seq (#a:Type) (#eq:CE.equiv a) (m:CE.cm a eq) (s:Seq.seq a) : Lemma (requires Seq.equal s (Seq.create (Seq.length s) m.unit)) (ensures eq.eq (foldm_snoc m s) m.unit) (decreases Seq.length s) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s = 0 then () else let s_tl, _ = un_snoc s in foldm_snoc_unit_seq m s_tl #push-options "--fuel 2" let foldm_snoc_singleton (#a:_) #eq (m:CE.cm a eq) (x:a) : Lemma (eq.eq (foldm_snoc m (Seq.create 1 x)) x) = elim_monoid_laws m #pop-options let x_yz_to_y_xz #a #eq (m:CE.cm a eq) (x y z:a) : Lemma ((x `m.mult` (y `m.mult` z)) `eq.eq` (y `m.mult` (x `m.mult` z))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { x `m.mult` (y `m.mult` z); (eq.eq) { m.commutativity x (y `m.mult` z) } (y `m.mult` z) `m.mult` x; (eq.eq) { m.associativity y z x } y `m.mult` (z `m.mult` x); (eq.eq) { m.congruence y (z `m.mult` x) y (x `m.mult` z) } y `m.mult` (x `m.mult` z); } #push-options "--fuel 1 --ifuel 0 --z3rlimit_factor 2" let rec foldm_snoc_append #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (m.mult (foldm_snoc m s1) (foldm_snoc m s2))) (decreases (Seq.length s2)) = CE.elim_eq_laws eq; elim_monoid_laws m; if Seq.length s2 = 0 then assert (Seq.append s1 s2 `Seq.equal` s1) else ( let s2', last = Seq.un_snoc s2 in calc (eq.eq) { foldm_snoc m (append s1 s2); (eq.eq) { assert (Seq.equal (append s1 s2) (Seq.snoc (append s1 s2') last)) } foldm_snoc m (Seq.snoc (append s1 s2') last); (eq.eq) { assert (Seq.equal (fst (Seq.un_snoc (append s1 s2))) (append s1 s2')) } m.mult last (foldm_snoc m (append s1 s2')); (eq.eq) { foldm_snoc_append m s1 s2'; m.congruence last (foldm_snoc m (append s1 s2')) last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')) } m.mult last (m.mult (foldm_snoc m s1) (foldm_snoc m s2')); (eq.eq) { x_yz_to_y_xz m last (foldm_snoc m s1) (foldm_snoc m s2') } m.mult (foldm_snoc m s1) (m.mult last (foldm_snoc m s2')); (eq.eq) { } m.mult (foldm_snoc m s1) (foldm_snoc m s2); }) #pop-options let foldm_snoc_sym #a #eq (m:CE.cm a eq) (s1 s2: seq a) : Lemma (ensures eq.eq (foldm_snoc m (append s1 s2)) (foldm_snoc m (append s2 s1))) = CE.elim_eq_laws eq; elim_monoid_laws m; foldm_snoc_append m s1 s2; foldm_snoc_append m s2 s1 #push-options "--fuel 0" let foldm_snoc3 #a #eq (m:CE.cm a eq) (s1:seq a) (x:a) (s2:seq a) : Lemma (eq.eq (foldm_snoc m (Seq.append s1 (Seq.cons x s2))) (m.mult x (foldm_snoc m (Seq.append s1 s2)))) = CE.elim_eq_laws eq; elim_monoid_laws m; calc (eq.eq) { foldm_snoc m (Seq.append s1 (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m s1 (Seq.cons x s2) } m.mult (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)); (eq.eq) { foldm_snoc_append m (Seq.create 1 x) s2; m.congruence (foldm_snoc m s1) (foldm_snoc m (Seq.cons x s2)) (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_singleton m x; m.congruence (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2) x (foldm_snoc m s2); m.congruence (foldm_snoc m s1) (m.mult (foldm_snoc m (Seq.create 1 x)) (foldm_snoc m s2)) (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)) } m.mult (foldm_snoc m s1) (m.mult x (foldm_snoc m s2)); (eq.eq) { x_yz_to_y_xz m (foldm_snoc m s1) x (foldm_snoc m s2) } m.mult x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)); (eq.eq) { foldm_snoc_append m s1 s2; m.congruence x (m.mult (foldm_snoc m s1) (foldm_snoc m s2)) x (foldm_snoc m (Seq.append s1 s2)) } m.mult x (foldm_snoc m (Seq.append s1 s2)); } #pop-options let remove_i #a (s:seq a) (i:nat{i < Seq.length s}) : a & seq a = let s0, s1 = Seq.split s i in Seq.head s1, Seq.append s0 (Seq.tail s1) #push-options "--using_facts_from '* -FStar.Seq.Properties.slice_slice'" let shift_perm' #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Tot (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) = reveal_is_permutation s0 s1 p; let s0', last = Seq.un_snoc s0 in let n = Seq.length s0' in let p' (i:nat{ i < n }) : j:nat{ j < n } = if p i < p n then p i else p i - 1 in let _, s1' = remove_i s1 (p n) in reveal_is_permutation_nopats s0' s1' p'; p' #pop-options let shift_perm #a (s0 s1:seq a) (_:squash (Seq.length s0 == Seq.length s1 /\ Seq.length s0 > 0)) (p:seqperm s0 s1) : Pure (seqperm (fst (Seq.un_snoc s0)) (snd (remove_i s1 (p (Seq.length s0 - 1))))) (requires True) (ensures fun _ -> let n = Seq.length s0 - 1 in Seq.index s1 (p n) == Seq.index s0 n) = reveal_is_permutation s0 s1 p; shift_perm' s0 s1 () p let seqperm_len #a (s0 s1:seq a) (p:seqperm s0 s1) : Lemma (ensures Seq.length s0 == Seq.length s1) = reveal_is_permutation s0 s1 p let eq2_eq #a (eq:CE.equiv a) (x y:a) : Lemma (requires x == y) (ensures x `eq.eq` y) = eq.reflexivity x (* The sequence indexing lemmas make this quite fiddly *) #push-options "--z3rlimit_factor 2 --fuel 1 --ifuel 0" let rec foldm_snoc_perm #a #eq m s0 s1 p : Lemma (ensures eq.eq (foldm_snoc m s0) (foldm_snoc m s1)) (decreases (Seq.length s0)) = //for getting calc chain to compose CE.elim_eq_laws eq; seqperm_len s0 s1 p; if Seq.length s0 = 0 then ( assert (Seq.equal s0 s1); eq2_eq eq (foldm_snoc m s0) (foldm_snoc m s1) ) else ( let n0 = Seq.length s0 - 1 in let prefix, last = Seq.un_snoc s0 in let prefix', suffix' = Seq.split s1 (p n0) in let last', suffix' = Seq.head suffix', Seq.tail suffix' in let s1' = snd (remove_i s1 (p n0)) in let p' : seqperm prefix s1' = shift_perm s0 s1 () p in assert (last == last'); calc (eq.eq) { foldm_snoc m s1; (eq.eq) { assert (s1 `Seq.equal` Seq.append prefix' (Seq.cons last' suffix')); eq2_eq eq (foldm_snoc m s1) (foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix'))) } foldm_snoc m (Seq.append prefix' (Seq.cons last' suffix')); (eq.eq) { foldm_snoc3 m prefix' last' suffix' } m.mult last' (foldm_snoc m (append prefix' suffix')); (eq.eq) { assert (Seq.equal (append prefix' suffix') s1'); eq2_eq eq (m.mult last' (foldm_snoc m (append prefix' suffix'))) (m.mult last' (foldm_snoc m s1')) } m.mult last' (foldm_snoc m s1'); (eq.eq) { foldm_snoc_perm m prefix s1' p'; eq.symmetry (foldm_snoc m prefix) (foldm_snoc m s1'); eq.reflexivity last'; m.congruence last' (foldm_snoc m s1') last' (foldm_snoc m prefix) } m.mult last' (foldm_snoc m prefix); (eq.eq) { eq2_eq eq (m.mult last' (foldm_snoc m prefix)) (foldm_snoc m s0) } foldm_snoc m s0; }; eq.symmetry (foldm_snoc m s1) (foldm_snoc m s0)) #pop-options //////////////////////////////////////////////////////////////////////////////// // foldm_snoc_split //////////////////////////////////////////////////////////////////////////////// (* Some utilities to introduce associativity-commutativity reasoning on CM using quantified formulas with patterns. Use these with care, since with large terms the SMT solver may end up with an explosion of instantiations *) let cm_associativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y z:c). {:pattern (x `cm.mult` y `cm.mult` z)} (x `cm.mult` y `cm.mult` z) `eq.eq` (x `cm.mult` (y `cm.mult` z))) = Classical.forall_intro_3 (Classical.move_requires_3 cm.associativity) let cm_commutativity #c #eq (cm: CE.cm c eq) : Lemma (forall (x y:c). {:pattern (x `cm.mult` y)} (x `cm.mult` y) `eq.eq` (y `cm.mult` x)) = Classical.forall_intro_2 (Classical.move_requires_2 cm.commutativity) (* A utility to introduce the equivalence relation laws into the context. FStar.Algebra.CommutativeMonoid provides something similar, but this version provides a more goal-directed pattern for transitivity. We should consider changing FStar.Algebra.CommutativeMonoid *) let elim_eq_laws #a (eq:CE.equiv a) : Lemma ( (forall x.{:pattern (x `eq.eq` x)} x `eq.eq` x) /\ (forall x y.{:pattern (x `eq.eq` y)} x `eq.eq` y ==> y `eq.eq` x) /\ (forall x y z.{:pattern eq.eq x y; eq.eq x z} (x `eq.eq` y /\ y `eq.eq` z) ==> x `eq.eq` z) ) = CE.elim_eq_laws eq let fold_decomposition_aux #c #eq (cm: CE.cm c eq) (n0: int) (nk: int{nk=n0}) (expr1 expr2: (ifrom_ito n0 nk) -> c) : Lemma (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk)) `eq.eq` cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)))) = elim_eq_laws eq; let sum_of_funcs (i: under (closed_interval_size n0 nk)) = expr1 (n0+i) `cm.mult` expr2 (n0+i) in lemma_eq_elim (init (closed_interval_size n0 nk) sum_of_funcs) (create 1 (expr1 n0 `cm.mult` expr2 n0)); foldm_snoc_singleton cm (expr1 n0 `cm.mult` expr2 n0); let ts = (init (closed_interval_size n0 nk) sum_of_funcs) in let ts1 = (init (nk+1-n0) (fun i -> expr1 (n0+i))) in let ts2 = (init (nk+1-n0) (fun i -> expr2 (n0+i))) in assert (foldm_snoc cm ts `eq.eq` sum_of_funcs (nk-n0)); // this assert speeds up the proof. foldm_snoc_singleton cm (expr1 nk); foldm_snoc_singleton cm (expr2 nk); cm.congruence (foldm_snoc cm ts1) (foldm_snoc cm ts2) (expr1 nk) (expr2 nk) let aux_shuffle_lemma #c #eq (cm: CE.cm c eq) (s1 s2 l1 l2: c) : Lemma (((s1 `cm.mult` s2) `cm.mult` (l1 `cm.mult` l2)) `eq.eq` ((s1 `cm.mult` l1) `cm.mult` (s2 `cm.mult` l2))) = elim_eq_laws eq; cm_commutativity cm; cm_associativity cm; let (+) = cm.mult in cm.congruence (s1+s2) l1 (s2+s1) l1; cm.congruence ((s1+s2)+l1) l2 ((s2+s1)+l1) l2; cm.congruence ((s2+s1)+l1) l2 (s2+(s1+l1)) l2; cm.congruence (s2+(s1+l1)) l2 ((s1+l1)+s2) l2 #push-options "--ifuel 0 --fuel 1 --z3rlimit 40" (* This proof is quite delicate, for several reasons: - It's working with higher order functions that are non-trivially dependently typed, notably on the ranges the ranges of indexes they manipulate - When using the induction hypothesis (i.e., on a recursive call), what we get is a property about the function at a different type, i.e., `range_count n0 (nk - 1)`. - If left to the SMT solver alone, these higher order functions at slightly different types cannot be proven equal and the proof fails, often mysteriously. - To have something more robust, I rewrote this function to return a squash proof, and then to coerce this proof to the type needed, where the F* unififer/normalization machinery can help, rather than leaving it purely to SMT, which is what happens when the property is states as a postcondition of a Lemma
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Seq.Permutation.fst" }
[ { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 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": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 40, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cm: FStar.Algebra.CommMonoid.Equiv.cm c eq -> n0: Prims.int -> nk: FStar.IntegerIntervals.not_less_than n0 -> expr1: (_: FStar.IntegerIntervals.ifrom_ito n0 nk -> c) -> expr2: (_: FStar.IntegerIntervals.ifrom_ito n0 nk -> c) -> Prims.Tot (Prims.squash (EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init (FStar.IntegerIntervals.closed_interval_size n0 nk) (FStar.Seq.Permutation.init_func_from_expr (FStar.Seq.Permutation.func_sum cm expr1 expr2) n0 nk))) (CM?.mult cm (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init (FStar.IntegerIntervals.closed_interval_size n0 nk) (FStar.Seq.Permutation.init_func_from_expr expr1 n0 nk))) (FStar.Seq.Permutation.foldm_snoc cm (FStar.Seq.Base.init (FStar.IntegerIntervals.closed_interval_size n0 nk) (FStar.Seq.Permutation.init_func_from_expr expr2 n0 nk))))))
Prims.Tot
[ "total", "" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "FStar.Algebra.CommMonoid.Equiv.cm", "Prims.int", "FStar.IntegerIntervals.not_less_than", "FStar.IntegerIntervals.ifrom_ito", "Prims.op_Equality", "FStar.Seq.Permutation.fold_decomposition_aux", "Prims.bool", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__congruence", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "FStar.Seq.Permutation.foldm_snoc", "Prims.unit", "FStar.Seq.Permutation.aux_shuffle_lemma", "Prims.squash", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Base.init", "FStar.Seq.Permutation.init_func_from_expr", "FStar.Calc.calc_finish", "Prims.eq2", "FStar.Seq.Permutation.func_sum", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "FStar.Calc.calc_step", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Seq.Permutation.foldm_snoc_split'", "Prims.op_Subtraction", "FStar.Seq.Base.lemma_eq_elim", "FStar.Pervasives.Native.fst", "FStar.Seq.Base.seq", "FStar.Seq.Properties.un_snoc", "FStar.IntegerIntervals.under", "FStar.IntegerIntervals.closed_interval_size", "Prims.nat", "FStar.Seq.Permutation.elim_eq_laws", "FStar.Seq.Permutation.cm_commutativity" ]
[ "recursion" ]
false
false
true
false
false
let rec foldm_snoc_split' #c #eq (cm: CE.cm c eq) (n0: int) (nk: not_less_than n0) (expr1: ((ifrom_ito n0 nk) -> c)) (expr2: ((ifrom_ito n0 nk) -> c)) : Tot (squash ((foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr (func_sum cm expr1 expr2) n0 nk))) `eq.eq` (cm.mult (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr1 n0 nk))) (foldm_snoc cm (init (closed_interval_size n0 nk) (init_func_from_expr expr2 n0 nk)) )))) (decreases nk - n0) =
if (nk = n0) then fold_decomposition_aux cm n0 nk expr1 expr2 else (cm_commutativity cm; elim_eq_laws eq; let lfunc_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr (func_sum cm expr1 expr2) n0 nf in let range_count = closed_interval_size in let full_count = range_count n0 nk in let sub_count = range_count n0 (nk - 1) in let fullseq = init full_count (lfunc_up_to nk) in let rfunc_1_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr1 n0 nf in let rfunc_2_up_to (nf: ifrom_ito n0 nk) = init_func_from_expr expr2 n0 nf in let fullseq_r1 = init full_count (rfunc_1_up_to nk) in let fullseq_r2 = init full_count (rfunc_2_up_to nk) in let subseq = init sub_count (lfunc_up_to nk) in let subfold = foldm_snoc cm subseq in let last = lfunc_up_to nk sub_count in lemma_eq_elim (fst (un_snoc fullseq)) subseq; let fullfold = foldm_snoc cm fullseq in let subseq_r1 = init sub_count (rfunc_1_up_to nk) in let subseq_r2 = init sub_count (rfunc_2_up_to nk) in lemma_eq_elim (fst (un_snoc fullseq_r1)) subseq_r1; lemma_eq_elim (fst (un_snoc fullseq_r2)) subseq_r2; lemma_eq_elim (init sub_count (lfunc_up_to nk)) subseq; lemma_eq_elim (init sub_count (lfunc_up_to (nk - 1))) subseq; lemma_eq_elim subseq_r1 (init sub_count (rfunc_1_up_to (nk - 1))); lemma_eq_elim subseq_r2 (init sub_count (rfunc_2_up_to (nk - 1))); let fullfold_r1 = foldm_snoc cm fullseq_r1 in let fullfold_r2 = foldm_snoc cm fullseq_r2 in let subfold_r1 = foldm_snoc cm subseq_r1 in let subfold_r2 = foldm_snoc cm subseq_r2 in cm.congruence (foldm_snoc cm (init sub_count (rfunc_1_up_to (nk - 1)))) (foldm_snoc cm (init sub_count (rfunc_2_up_to (nk - 1)))) subfold_r1 subfold_r2; let last_r1 = rfunc_1_up_to nk sub_count in let last_r2 = rfunc_2_up_to nk sub_count in let nk' = nk - 1 in let ih:squash (((foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk'))) `eq.eq` (cm.mult (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))) (foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk')))))) = foldm_snoc_split' cm n0 nk' expr1 expr2 in calc ( == ) { subfold; ( == ) { () } foldm_snoc cm subseq; ( == ) { assert (Seq.equal subseq (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk'))) } foldm_snoc cm (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' (func_sum #(ifrom_ito n0 nk') cm expr1 expr2) n0 nk')); }; assert (Seq.equal subseq_r1 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr1 n0 nk'))); assert (Seq.equal subseq_r2 (init (range_count n0 nk') (init_func_from_expr #_ #n0 #nk' expr2 n0 nk'))); let _:squash (subfold `eq.eq` (subfold_r1 `cm.mult` subfold_r2)) = ih in cm.congruence subfold last (subfold_r1 `cm.mult` subfold_r2) last; aux_shuffle_lemma cm subfold_r1 subfold_r2 (rfunc_1_up_to nk sub_count) (rfunc_2_up_to nk sub_count); cm.congruence (subfold_r1 `cm.mult` (rfunc_1_up_to nk sub_count)) (subfold_r2 `cm.mult` (rfunc_2_up_to nk sub_count)) (foldm_snoc cm fullseq_r1) (foldm_snoc cm fullseq_r2))
false
LowParseWriters.fsti
LowParseWriters.failwith_repr
val failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv)
val failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv)
let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 36, "end_line": 540, "start_col": 0, "start_line": 535 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> inv: LowParseWriters.memory_invariant -> s: Prims.string -> LowParseWriters.read_repr a Prims.l_True (fun _ -> Prims.l_False) (fun _ -> Prims.l_True) inv
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.memory_invariant", "Prims.string", "LowParseWriters.ReadRepr", "Prims.l_True", "Prims.l_False", "Prims.unit", "LowParseWriters.failwith_spec", "LowParseWriters.failwith_impl", "LowParseWriters.read_repr" ]
[]
false
false
false
false
false
let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) =
ReadRepr _ (failwith_impl a inv s)
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.fsqrt
val fsqrt: res:felem -> a:felem -> Stack unit (requires fun h -> live h a /\ live h res /\ eq_or_disjoint a res /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc res) h0 h1 /\ as_nat h1 res < S.prime /\ fmont_as_nat h1 res = S.fsqrt (fmont_as_nat h0 a))
val fsqrt: res:felem -> a:felem -> Stack unit (requires fun h -> live h a /\ live h res /\ eq_or_disjoint a res /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc res) h0 h1 /\ as_nat h1 res < S.prime /\ fmont_as_nat h1 res = S.fsqrt (fmont_as_nat h0 a))
let fsqrt res a = let h0 = ST.get () in push_frame (); let tmp = create 8ul (u64 0) in let tmp1 = sub tmp 0ul 4ul in let tmp2 = sub tmp 4ul 4ul in fsqrt_254 tmp2 tmp1 a; copy res tmp2; let h1 = ST.get () in assert (fmont_as_nat h1 res == SI.fsqrt (fmont_as_nat h0 a)); SI.fsqrt_is_fsqrt_lemma (fmont_as_nat h0 a); pop_frame ()
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 313, "start_col": 0, "start_line": 302 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let one_mod ctx one = make_fone one inline_for_extraction noextract val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let mul_mod ctx x y xy = fmul xy x y inline_for_extraction noextract val sqr_mod : BE.lsqr_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let sqr_mod ctx x xx = fsqr xx x inline_for_extraction noextract let mk_p256_prime_concrete_ops : BE.concrete_ops U64 4ul 0ul = { BE.to = mk_to_p256_prime_comm_monoid; BE.lone = one_mod; BE.lmul = mul_mod; BE.lsqr = sqr_mod; } inline_for_extraction noextract val fsquare_times_in_place (out:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ as_nat h out < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 out) (v b)) let fsquare_times_in_place out b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 out) (v b); BE.lexp_pow2_in_place 4ul 0ul mk_p256_prime_concrete_ops (null uint64) out b inline_for_extraction noextract val fsquare_times (out a:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ live h a /\ disjoint out a /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 a) (v b)) let fsquare_times out a b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 a) (v b); BE.lexp_pow2 4ul 0ul mk_p256_prime_concrete_ops (null uint64) a b out inline_for_extraction noextract val finv_30 (x30 x2 tmp1 tmp2 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h tmp1 /\ live h tmp2 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint x30 x2 /\ disjoint x30 tmp1 /\ disjoint x30 tmp2 /\ disjoint x2 tmp1 /\ disjoint x2 tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc x30 |+| loc x2 |+| loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 x30 < S.prime /\ as_nat h1 x2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2_s = S.fmul (SI.fsquare_times f 1) f in let x3_s = S.fmul (SI.fsquare_times x2_s 1) f in let x6_s = S.fmul (SI.fsquare_times x3_s 3) x3_s in let x12_s = S.fmul (SI.fsquare_times x6_s 6) x6_s in let x15_s = S.fmul (SI.fsquare_times x12_s 3) x3_s in let x30_s = S.fmul (SI.fsquare_times x15_s 15) x15_s in fmont_as_nat h1 x30 = x30_s /\ fmont_as_nat h1 x2 = x2_s)) let finv_30 x30 x2 tmp1 tmp2 a = let h0 = ST.get () in fsquare_times x2 a 1ul; fmul x2 x2 a; let h1 = ST.get () in assert (fmont_as_nat h1 x2 == S.fmul (SI.fsquare_times (fmont_as_nat h0 a) 1) (fmont_as_nat h0 a)); fsquare_times x30 x2 1ul; fmul x30 x30 a; let h2 = ST.get () in assert (fmont_as_nat h2 x30 == // x3 S.fmul (SI.fsquare_times (fmont_as_nat h1 x2) 1) (fmont_as_nat h0 a)); fsquare_times tmp1 x30 3ul; fmul tmp1 tmp1 x30; let h3 = ST.get () in assert (fmont_as_nat h3 tmp1 == // x6 S.fmul (SI.fsquare_times (fmont_as_nat h2 x30) 3) (fmont_as_nat h2 x30)); fsquare_times tmp2 tmp1 6ul; fmul tmp2 tmp2 tmp1; let h4 = ST.get () in assert (fmont_as_nat h4 tmp2 == // x12 S.fmul (SI.fsquare_times (fmont_as_nat h3 tmp1) 6) (fmont_as_nat h3 tmp1)); fsquare_times tmp1 tmp2 3ul; fmul tmp1 tmp1 x30; let h5 = ST.get () in assert (fmont_as_nat h5 tmp1 == // x15 S.fmul (SI.fsquare_times (fmont_as_nat h4 tmp2) 3) (fmont_as_nat h2 x30)); fsquare_times x30 tmp1 15ul; fmul x30 x30 tmp1; let h6 = ST.get () in assert (fmont_as_nat h6 x30 == // x30 S.fmul (SI.fsquare_times (fmont_as_nat h5 tmp1) 15) (fmont_as_nat h5 tmp1)) inline_for_extraction noextract val finv_256 (x256 x2 x30 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h x256 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a x256 /\ disjoint x30 x2 /\ disjoint x30 x256 /\ disjoint x2 x256 /\ as_nat h a < S.prime /\ as_nat h x30 < S.prime /\ as_nat h x2 < S.prime) (ensures fun h0 _ h1 -> modifies (loc x256 |+| loc x2) h0 h1 /\ as_nat h1 x256 < S.prime /\ (let f = fmont_as_nat h0 a in let x30 = fmont_as_nat h0 x30 in let x2 = fmont_as_nat h0 x2 in let x32_s = S.fmul (SI.fsquare_times x30 2) x2 in let x64_s = S.fmul (SI.fsquare_times x32_s 32) f in let x192_s = S.fmul (SI.fsquare_times x64_s 128) x32_s in let x224_s = S.fmul (SI.fsquare_times x192_s 32) x32_s in let x254_s = S.fmul (SI.fsquare_times x224_s 30) x30 in let x256_s = S.fmul (SI.fsquare_times x254_s 2) f in fmont_as_nat h1 x256 = x256_s)) let finv_256 x256 x2 x30 a = let h0 = ST.get () in fsquare_times x256 x30 2ul; fmul x256 x256 x2; let h1 = ST.get () in assert (fmont_as_nat h1 x256 == // x32 S.fmul (SI.fsquare_times (fmont_as_nat h0 x30) 2) (fmont_as_nat h0 x2)); fsquare_times x2 x256 32ul; fmul x2 x2 a; let h2 = ST.get () in assert (fmont_as_nat h2 x2 == // x64 S.fmul (SI.fsquare_times (fmont_as_nat h1 x256) 32) (fmont_as_nat h0 a)); fsquare_times_in_place x2 128ul; fmul x2 x2 x256; let h3 = ST.get () in assert (fmont_as_nat h3 x2 == // x192 S.fmul (SI.fsquare_times (fmont_as_nat h2 x2) 128) (fmont_as_nat h1 x256)); fsquare_times_in_place x2 32ul; fmul x2 x2 x256; let h4 = ST.get () in assert (fmont_as_nat h4 x2 == // x224 S.fmul (SI.fsquare_times (fmont_as_nat h3 x2) 32) (fmont_as_nat h1 x256)); fsquare_times_in_place x2 30ul; fmul x2 x2 x30; let h5 = ST.get () in assert (fmont_as_nat h5 x2 == // x254 S.fmul (SI.fsquare_times (fmont_as_nat h4 x2) 30) (fmont_as_nat h0 x30)); fsquare_times_in_place x2 2ul; fmul x256 x2 a; let h6 = ST.get () in assert (fmont_as_nat h6 x256 == // x256 S.fmul (SI.fsquare_times (fmont_as_nat h5 x2) 2) (fmont_as_nat h0 a)) [@CInline] let finv res a = let h0 = ST.get () in push_frame (); let tmp = create 16ul (u64 0) in let x30 = sub tmp 0ul 4ul in let x2 = sub tmp 4ul 4ul in let tmp1 = sub tmp 8ul 4ul in let tmp2 = sub tmp 12ul 4ul in finv_30 x30 x2 tmp1 tmp2 a; finv_256 tmp1 x2 x30 a; copy res tmp1; let h1 = ST.get () in assert (fmont_as_nat h1 res == SI.finv (fmont_as_nat h0 a)); SI.finv_is_finv_lemma (fmont_as_nat h0 a); pop_frame () inline_for_extraction noextract val fsqrt_254 (tmp2 tmp1 a:felem) : Stack unit (requires fun h -> live h a /\ live h tmp1 /\ live h tmp2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 tmp2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2 = S.fmul (SI.fsquare_times f 1) f in let x4 = S.fmul (SI.fsquare_times x2 2) x2 in let x8 = S.fmul (SI.fsquare_times x4 4) x4 in let x16 = S.fmul (SI.fsquare_times x8 8) x8 in let x32 = S.fmul (SI.fsquare_times x16 16) x16 in let x64 = S.fmul (SI.fsquare_times x32 32) f in let x160 = S.fmul (SI.fsquare_times x64 96) f in let x254 = SI.fsquare_times x160 94 in fmont_as_nat h1 tmp2 == x254)) let fsqrt_254 tmp2 tmp1 a = let h0 = ST.get () in fsquare_times tmp1 a 1ul; fmul tmp1 tmp1 a; let h1 = ST.get () in assert (fmont_as_nat h1 tmp1 == // x2 S.fmul (SI.fsquare_times (fmont_as_nat h0 a) 1) (fmont_as_nat h0 a)); fsquare_times tmp2 tmp1 2ul; fmul tmp2 tmp2 tmp1; let h2 = ST.get () in assert (fmont_as_nat h2 tmp2 == // x4 S.fmul (SI.fsquare_times (fmont_as_nat h1 tmp1) 2) (fmont_as_nat h1 tmp1)); fsquare_times tmp1 tmp2 4ul; fmul tmp1 tmp1 tmp2; let h3 = ST.get () in assert (fmont_as_nat h3 tmp1 == // x8 S.fmul (SI.fsquare_times (fmont_as_nat h2 tmp2) 4) (fmont_as_nat h2 tmp2)); fsquare_times tmp2 tmp1 8ul; fmul tmp2 tmp2 tmp1; let h4 = ST.get () in assert (fmont_as_nat h4 tmp2 == // x16 S.fmul (SI.fsquare_times (fmont_as_nat h3 tmp1) 8) (fmont_as_nat h3 tmp1)); fsquare_times tmp1 tmp2 16ul; fmul tmp1 tmp1 tmp2; let h5 = ST.get () in assert (fmont_as_nat h5 tmp1 == // x32 S.fmul (SI.fsquare_times (fmont_as_nat h4 tmp2) 16) (fmont_as_nat h4 tmp2)); fsquare_times tmp2 tmp1 32ul; fmul tmp2 tmp2 a; let h6 = ST.get () in assert (fmont_as_nat h6 tmp2 == // x64 S.fmul (SI.fsquare_times (fmont_as_nat h5 tmp1) 32) (fmont_as_nat h0 a)); fsquare_times_in_place tmp2 96ul; fmul tmp2 tmp2 a; let h7 = ST.get () in assert (fmont_as_nat h7 tmp2 == // x160 S.fmul (SI.fsquare_times (fmont_as_nat h6 tmp2) 96) (fmont_as_nat h0 a)); fsquare_times_in_place tmp2 94ul
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
res: Hacl.Impl.P256.Bignum.felem -> a: Hacl.Impl.P256.Bignum.felem -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Impl.P256.Bignum.felem", "FStar.HyperStack.ST.pop_frame", "Prims.unit", "Hacl.Spec.P256.Finv.fsqrt_is_fsqrt_lemma", "Hacl.Impl.P256.Field.fmont_as_nat", "Prims._assert", "Prims.eq2", "Spec.P256.PointOps.felem", "Hacl.Spec.P256.Finv.fsqrt", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "Lib.Buffer.copy", "Lib.Buffer.MUT", "Lib.IntTypes.uint64", "Lib.IntTypes.size", "Hacl.Impl.P256.Finv.fsqrt_254", "Lib.Buffer.lbuffer_t", "Lib.IntTypes.int_t", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "FStar.UInt32.uint_to_t", "FStar.UInt32.t", "Lib.Buffer.sub", "FStar.UInt32.__uint_to_t", "Lib.Buffer.create", "Lib.IntTypes.u64", "Lib.Buffer.lbuffer", "FStar.HyperStack.ST.push_frame" ]
[]
false
true
false
false
false
let fsqrt res a =
let h0 = ST.get () in push_frame (); let tmp = create 8ul (u64 0) in let tmp1 = sub tmp 0ul 4ul in let tmp2 = sub tmp 4ul 4ul in fsqrt_254 tmp2 tmp1 a; copy res tmp2; let h1 = ST.get () in assert (fmont_as_nat h1 res == SI.fsqrt (fmont_as_nat h0 a)); SI.fsqrt_is_fsqrt_lemma (fmont_as_nat h0 a); pop_frame ()
false
LowParseWriters.fsti
LowParseWriters.ptr
val ptr : p: LowParseWriters.LowParse.parser -> inv: LowParseWriters.memory_invariant -> Type0
let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x })
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 34, "end_line": 658, "start_col": 0, "start_line": 657 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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: LowParseWriters.LowParse.parser -> inv: LowParseWriters.memory_invariant -> Type0
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.memory_invariant", "LowParseWriters.rptr", "LowParseWriters.valid_rptr" ]
[]
false
false
false
true
true
let ptr (p: parser) (inv: memory_invariant) =
(x: rptr{valid_rptr p inv x})
false
LowParseWriters.fsti
LowParseWriters.read_repr_spec
val read_repr_spec (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x)
val read_repr_spec (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x)
let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () ))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 77, "start_col": 0, "start_line": 69 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> pre: Prims.pure_pre -> post: LowParseWriters.pure_post' a pre -> post_err: LowParseWriters.pure_post_err pre -> Type
Prims.Tot
[ "total" ]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "Prims.unit", "LowParseWriters.result", "Prims.string" ]
[]
false
false
false
false
true
let read_repr_spec (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) =
unit -> Ghost (result a) (requires pre) (ensures (function | Correct v -> post v | Error _ -> post_err ()))
false
LowParseWriters.fsti
LowParseWriters.read_bind_spec
val read_bind_spec (a b: Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x: a -> pure_pre)) (post_g: (x: a -> pure_post' b (pre_g x))) (post_err_g: (x: a -> pure_post_err (pre_g x))) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g: (x: a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a). post_f x ==> pre_g x)) (fun y -> exists (x: a). pre_f /\ post_f x /\ post_g x y) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a). post_f x /\ post_err_g x ()))))
val read_bind_spec (a b: Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x: a -> pure_pre)) (post_g: (x: a -> pure_post' b (pre_g x))) (post_err_g: (x: a -> pure_post_err (pre_g x))) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g: (x: a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a). post_f x ==> pre_g x)) (fun y -> exists (x: a). pre_f /\ post_f x /\ post_g x y) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a). post_f x /\ post_err_g x ()))))
let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 203, "start_col": 0, "start_line": 187 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ()))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> b: Type -> pre_f: Prims.pure_pre -> post_f: LowParseWriters.pure_post' a pre_f -> post_err_f: LowParseWriters.pure_post_err pre_f -> pre_g: (x: a -> Prims.pure_pre) -> post_g: (x: a -> LowParseWriters.pure_post' b (pre_g x)) -> post_err_g: (x: a -> LowParseWriters.pure_post_err (pre_g x)) -> f_bind_spec: LowParseWriters.read_repr_spec a pre_f post_f post_err_f -> g: (x: a -> LowParseWriters.read_repr_spec b (pre_g x) (post_g x) (post_err_g x)) -> LowParseWriters.read_repr_spec b (pre_f /\ (forall (x: a). post_f x ==> pre_g x)) (fun y -> exists (x: a). pre_f /\ post_f x /\ post_g x y) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a). post_f x /\ post_err_g x ())))
Prims.Tot
[ "total" ]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "LowParseWriters.read_repr_spec", "Prims.unit", "Prims.string", "LowParseWriters.Error", "LowParseWriters.result", "Prims.l_and", "Prims.l_Forall", "Prims.l_imp", "Prims.l_Exists", "Prims.l_or" ]
[]
false
false
false
false
false
let read_bind_spec (a b: Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x: a -> pure_pre)) (post_g: (x: a -> pure_post' b (pre_g x))) (post_err_g: (x: a -> pure_post_err (pre_g x))) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g: (x: a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a). post_f x ==> pre_g x)) (fun y -> exists (x: a). pre_f /\ post_f x /\ post_g x y) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a). post_f x /\ post_err_g x ())))) =
fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.fsqrt_254
val fsqrt_254 (tmp2 tmp1 a:felem) : Stack unit (requires fun h -> live h a /\ live h tmp1 /\ live h tmp2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 tmp2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2 = S.fmul (SI.fsquare_times f 1) f in let x4 = S.fmul (SI.fsquare_times x2 2) x2 in let x8 = S.fmul (SI.fsquare_times x4 4) x4 in let x16 = S.fmul (SI.fsquare_times x8 8) x8 in let x32 = S.fmul (SI.fsquare_times x16 16) x16 in let x64 = S.fmul (SI.fsquare_times x32 32) f in let x160 = S.fmul (SI.fsquare_times x64 96) f in let x254 = SI.fsquare_times x160 94 in fmont_as_nat h1 tmp2 == x254))
val fsqrt_254 (tmp2 tmp1 a:felem) : Stack unit (requires fun h -> live h a /\ live h tmp1 /\ live h tmp2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 tmp2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2 = S.fmul (SI.fsquare_times f 1) f in let x4 = S.fmul (SI.fsquare_times x2 2) x2 in let x8 = S.fmul (SI.fsquare_times x4 4) x4 in let x16 = S.fmul (SI.fsquare_times x8 8) x8 in let x32 = S.fmul (SI.fsquare_times x16 16) x16 in let x64 = S.fmul (SI.fsquare_times x32 32) f in let x160 = S.fmul (SI.fsquare_times x64 96) f in let x254 = SI.fsquare_times x160 94 in fmont_as_nat h1 tmp2 == x254))
let fsqrt_254 tmp2 tmp1 a = let h0 = ST.get () in fsquare_times tmp1 a 1ul; fmul tmp1 tmp1 a; let h1 = ST.get () in assert (fmont_as_nat h1 tmp1 == // x2 S.fmul (SI.fsquare_times (fmont_as_nat h0 a) 1) (fmont_as_nat h0 a)); fsquare_times tmp2 tmp1 2ul; fmul tmp2 tmp2 tmp1; let h2 = ST.get () in assert (fmont_as_nat h2 tmp2 == // x4 S.fmul (SI.fsquare_times (fmont_as_nat h1 tmp1) 2) (fmont_as_nat h1 tmp1)); fsquare_times tmp1 tmp2 4ul; fmul tmp1 tmp1 tmp2; let h3 = ST.get () in assert (fmont_as_nat h3 tmp1 == // x8 S.fmul (SI.fsquare_times (fmont_as_nat h2 tmp2) 4) (fmont_as_nat h2 tmp2)); fsquare_times tmp2 tmp1 8ul; fmul tmp2 tmp2 tmp1; let h4 = ST.get () in assert (fmont_as_nat h4 tmp2 == // x16 S.fmul (SI.fsquare_times (fmont_as_nat h3 tmp1) 8) (fmont_as_nat h3 tmp1)); fsquare_times tmp1 tmp2 16ul; fmul tmp1 tmp1 tmp2; let h5 = ST.get () in assert (fmont_as_nat h5 tmp1 == // x32 S.fmul (SI.fsquare_times (fmont_as_nat h4 tmp2) 16) (fmont_as_nat h4 tmp2)); fsquare_times tmp2 tmp1 32ul; fmul tmp2 tmp2 a; let h6 = ST.get () in assert (fmont_as_nat h6 tmp2 == // x64 S.fmul (SI.fsquare_times (fmont_as_nat h5 tmp1) 32) (fmont_as_nat h0 a)); fsquare_times_in_place tmp2 96ul; fmul tmp2 tmp2 a; let h7 = ST.get () in assert (fmont_as_nat h7 tmp2 == // x160 S.fmul (SI.fsquare_times (fmont_as_nat h6 tmp2) 96) (fmont_as_nat h0 a)); fsquare_times_in_place tmp2 94ul
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 34, "end_line": 298, "start_col": 0, "start_line": 254 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let one_mod ctx one = make_fone one inline_for_extraction noextract val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let mul_mod ctx x y xy = fmul xy x y inline_for_extraction noextract val sqr_mod : BE.lsqr_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let sqr_mod ctx x xx = fsqr xx x inline_for_extraction noextract let mk_p256_prime_concrete_ops : BE.concrete_ops U64 4ul 0ul = { BE.to = mk_to_p256_prime_comm_monoid; BE.lone = one_mod; BE.lmul = mul_mod; BE.lsqr = sqr_mod; } inline_for_extraction noextract val fsquare_times_in_place (out:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ as_nat h out < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 out) (v b)) let fsquare_times_in_place out b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 out) (v b); BE.lexp_pow2_in_place 4ul 0ul mk_p256_prime_concrete_ops (null uint64) out b inline_for_extraction noextract val fsquare_times (out a:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ live h a /\ disjoint out a /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 a) (v b)) let fsquare_times out a b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 a) (v b); BE.lexp_pow2 4ul 0ul mk_p256_prime_concrete_ops (null uint64) a b out inline_for_extraction noextract val finv_30 (x30 x2 tmp1 tmp2 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h tmp1 /\ live h tmp2 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint x30 x2 /\ disjoint x30 tmp1 /\ disjoint x30 tmp2 /\ disjoint x2 tmp1 /\ disjoint x2 tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc x30 |+| loc x2 |+| loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 x30 < S.prime /\ as_nat h1 x2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2_s = S.fmul (SI.fsquare_times f 1) f in let x3_s = S.fmul (SI.fsquare_times x2_s 1) f in let x6_s = S.fmul (SI.fsquare_times x3_s 3) x3_s in let x12_s = S.fmul (SI.fsquare_times x6_s 6) x6_s in let x15_s = S.fmul (SI.fsquare_times x12_s 3) x3_s in let x30_s = S.fmul (SI.fsquare_times x15_s 15) x15_s in fmont_as_nat h1 x30 = x30_s /\ fmont_as_nat h1 x2 = x2_s)) let finv_30 x30 x2 tmp1 tmp2 a = let h0 = ST.get () in fsquare_times x2 a 1ul; fmul x2 x2 a; let h1 = ST.get () in assert (fmont_as_nat h1 x2 == S.fmul (SI.fsquare_times (fmont_as_nat h0 a) 1) (fmont_as_nat h0 a)); fsquare_times x30 x2 1ul; fmul x30 x30 a; let h2 = ST.get () in assert (fmont_as_nat h2 x30 == // x3 S.fmul (SI.fsquare_times (fmont_as_nat h1 x2) 1) (fmont_as_nat h0 a)); fsquare_times tmp1 x30 3ul; fmul tmp1 tmp1 x30; let h3 = ST.get () in assert (fmont_as_nat h3 tmp1 == // x6 S.fmul (SI.fsquare_times (fmont_as_nat h2 x30) 3) (fmont_as_nat h2 x30)); fsquare_times tmp2 tmp1 6ul; fmul tmp2 tmp2 tmp1; let h4 = ST.get () in assert (fmont_as_nat h4 tmp2 == // x12 S.fmul (SI.fsquare_times (fmont_as_nat h3 tmp1) 6) (fmont_as_nat h3 tmp1)); fsquare_times tmp1 tmp2 3ul; fmul tmp1 tmp1 x30; let h5 = ST.get () in assert (fmont_as_nat h5 tmp1 == // x15 S.fmul (SI.fsquare_times (fmont_as_nat h4 tmp2) 3) (fmont_as_nat h2 x30)); fsquare_times x30 tmp1 15ul; fmul x30 x30 tmp1; let h6 = ST.get () in assert (fmont_as_nat h6 x30 == // x30 S.fmul (SI.fsquare_times (fmont_as_nat h5 tmp1) 15) (fmont_as_nat h5 tmp1)) inline_for_extraction noextract val finv_256 (x256 x2 x30 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h x256 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a x256 /\ disjoint x30 x2 /\ disjoint x30 x256 /\ disjoint x2 x256 /\ as_nat h a < S.prime /\ as_nat h x30 < S.prime /\ as_nat h x2 < S.prime) (ensures fun h0 _ h1 -> modifies (loc x256 |+| loc x2) h0 h1 /\ as_nat h1 x256 < S.prime /\ (let f = fmont_as_nat h0 a in let x30 = fmont_as_nat h0 x30 in let x2 = fmont_as_nat h0 x2 in let x32_s = S.fmul (SI.fsquare_times x30 2) x2 in let x64_s = S.fmul (SI.fsquare_times x32_s 32) f in let x192_s = S.fmul (SI.fsquare_times x64_s 128) x32_s in let x224_s = S.fmul (SI.fsquare_times x192_s 32) x32_s in let x254_s = S.fmul (SI.fsquare_times x224_s 30) x30 in let x256_s = S.fmul (SI.fsquare_times x254_s 2) f in fmont_as_nat h1 x256 = x256_s)) let finv_256 x256 x2 x30 a = let h0 = ST.get () in fsquare_times x256 x30 2ul; fmul x256 x256 x2; let h1 = ST.get () in assert (fmont_as_nat h1 x256 == // x32 S.fmul (SI.fsquare_times (fmont_as_nat h0 x30) 2) (fmont_as_nat h0 x2)); fsquare_times x2 x256 32ul; fmul x2 x2 a; let h2 = ST.get () in assert (fmont_as_nat h2 x2 == // x64 S.fmul (SI.fsquare_times (fmont_as_nat h1 x256) 32) (fmont_as_nat h0 a)); fsquare_times_in_place x2 128ul; fmul x2 x2 x256; let h3 = ST.get () in assert (fmont_as_nat h3 x2 == // x192 S.fmul (SI.fsquare_times (fmont_as_nat h2 x2) 128) (fmont_as_nat h1 x256)); fsquare_times_in_place x2 32ul; fmul x2 x2 x256; let h4 = ST.get () in assert (fmont_as_nat h4 x2 == // x224 S.fmul (SI.fsquare_times (fmont_as_nat h3 x2) 32) (fmont_as_nat h1 x256)); fsquare_times_in_place x2 30ul; fmul x2 x2 x30; let h5 = ST.get () in assert (fmont_as_nat h5 x2 == // x254 S.fmul (SI.fsquare_times (fmont_as_nat h4 x2) 30) (fmont_as_nat h0 x30)); fsquare_times_in_place x2 2ul; fmul x256 x2 a; let h6 = ST.get () in assert (fmont_as_nat h6 x256 == // x256 S.fmul (SI.fsquare_times (fmont_as_nat h5 x2) 2) (fmont_as_nat h0 a)) [@CInline] let finv res a = let h0 = ST.get () in push_frame (); let tmp = create 16ul (u64 0) in let x30 = sub tmp 0ul 4ul in let x2 = sub tmp 4ul 4ul in let tmp1 = sub tmp 8ul 4ul in let tmp2 = sub tmp 12ul 4ul in finv_30 x30 x2 tmp1 tmp2 a; finv_256 tmp1 x2 x30 a; copy res tmp1; let h1 = ST.get () in assert (fmont_as_nat h1 res == SI.finv (fmont_as_nat h0 a)); SI.finv_is_finv_lemma (fmont_as_nat h0 a); pop_frame () inline_for_extraction noextract val fsqrt_254 (tmp2 tmp1 a:felem) : Stack unit (requires fun h -> live h a /\ live h tmp1 /\ live h tmp2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 tmp2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2 = S.fmul (SI.fsquare_times f 1) f in let x4 = S.fmul (SI.fsquare_times x2 2) x2 in let x8 = S.fmul (SI.fsquare_times x4 4) x4 in let x16 = S.fmul (SI.fsquare_times x8 8) x8 in let x32 = S.fmul (SI.fsquare_times x16 16) x16 in let x64 = S.fmul (SI.fsquare_times x32 32) f in let x160 = S.fmul (SI.fsquare_times x64 96) f in let x254 = SI.fsquare_times x160 94 in fmont_as_nat h1 tmp2 == x254))
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
tmp2: Hacl.Impl.P256.Bignum.felem -> tmp1: Hacl.Impl.P256.Bignum.felem -> a: Hacl.Impl.P256.Bignum.felem -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Impl.P256.Bignum.felem", "Hacl.Impl.P256.Finv.fsquare_times_in_place", "FStar.UInt32.__uint_to_t", "Prims.unit", "Prims._assert", "Prims.eq2", "Spec.P256.PointOps.felem", "Hacl.Impl.P256.Field.fmont_as_nat", "Spec.P256.PointOps.fmul", "Hacl.Spec.P256.Finv.fsquare_times", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "Hacl.Impl.P256.Field.fmul", "Hacl.Impl.P256.Finv.fsquare_times" ]
[]
false
true
false
false
false
let fsqrt_254 tmp2 tmp1 a =
let h0 = ST.get () in fsquare_times tmp1 a 1ul; fmul tmp1 tmp1 a; let h1 = ST.get () in assert (fmont_as_nat h1 tmp1 == S.fmul (SI.fsquare_times (fmont_as_nat h0 a) 1) (fmont_as_nat h0 a)); fsquare_times tmp2 tmp1 2ul; fmul tmp2 tmp2 tmp1; let h2 = ST.get () in assert (fmont_as_nat h2 tmp2 == S.fmul (SI.fsquare_times (fmont_as_nat h1 tmp1) 2) (fmont_as_nat h1 tmp1)); fsquare_times tmp1 tmp2 4ul; fmul tmp1 tmp1 tmp2; let h3 = ST.get () in assert (fmont_as_nat h3 tmp1 == S.fmul (SI.fsquare_times (fmont_as_nat h2 tmp2) 4) (fmont_as_nat h2 tmp2)); fsquare_times tmp2 tmp1 8ul; fmul tmp2 tmp2 tmp1; let h4 = ST.get () in assert (fmont_as_nat h4 tmp2 == S.fmul (SI.fsquare_times (fmont_as_nat h3 tmp1) 8) (fmont_as_nat h3 tmp1)); fsquare_times tmp1 tmp2 16ul; fmul tmp1 tmp1 tmp2; let h5 = ST.get () in assert (fmont_as_nat h5 tmp1 == S.fmul (SI.fsquare_times (fmont_as_nat h4 tmp2) 16) (fmont_as_nat h4 tmp2)); fsquare_times tmp2 tmp1 32ul; fmul tmp2 tmp2 a; let h6 = ST.get () in assert (fmont_as_nat h6 tmp2 == S.fmul (SI.fsquare_times (fmont_as_nat h5 tmp1) 32) (fmont_as_nat h0 a)); fsquare_times_in_place tmp2 96ul; fmul tmp2 tmp2 a; let h7 = ST.get () in assert (fmont_as_nat h7 tmp2 == S.fmul (SI.fsquare_times (fmont_as_nat h6 tmp2) 96) (fmont_as_nat h0 a)); fsquare_times_in_place tmp2 94ul
false
LowParseWriters.fsti
LowParseWriters.read_subcomp_spec
val read_subcomp_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp: read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True))
val read_subcomp_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp: read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True))
let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 24, "end_line": 268, "start_col": 0, "start_line": 261 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err'
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> pre: Prims.pure_pre -> post: LowParseWriters.pure_post' a pre -> post_err: LowParseWriters.pure_post_err pre -> pre': Prims.pure_pre -> post': LowParseWriters.pure_post' a pre' -> post_err': LowParseWriters.pure_post_err pre' -> f_subcomp: LowParseWriters.read_repr_spec a pre post post_err -> Prims.Pure (LowParseWriters.read_repr_spec a pre' post' post_err')
Prims.Pure
[]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "LowParseWriters.read_repr_spec", "Prims.unit", "LowParseWriters.result", "LowParseWriters.read_subcomp_spec_cond", "Prims.l_True" ]
[]
false
false
false
false
false
let read_subcomp_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp: read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) =
(fun x -> f_subcomp x)
false
LowParseWriters.fsti
LowParseWriters.validate_post_err
val validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b))
val validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b))
let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 56, "end_line": 793, "start_col": 0, "start_line": 788 }
(* Copyright 2019 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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: LowParseWriters.LowParse.parser -> inv: LowParseWriters.memory_invariant -> b: LowStar.Buffer.buffer FStar.UInt8.t -> LowParseWriters.pure_post_err (LowParseWriters.validate_pre inv b)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.memory_invariant", "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.unit", "Prims.l_Forall", "FStar.UInt32.t", "Prims.l_not", "LowParseWriters.LowParse.valid_pos", "FStar.Ghost.reveal", "FStar.Monotonic.HyperStack.mem", "LowParseWriters.__proj__Mkmemory_invariant__item__h0", "FStar.UInt32.__uint_to_t", "LowParseWriters.pure_post_err", "LowParseWriters.validate_pre" ]
[]
false
false
false
false
false
let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) =
fun _ -> forall pos. ~(valid_pos p inv.h0 b 0ul pos)
false
LowParseWriters.fsti
LowParseWriters.validate_pre
val validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre
val validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre
let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 17, "end_line": 775, "start_col": 0, "start_line": 769 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
inv: LowParseWriters.memory_invariant -> b: LowStar.Buffer.buffer FStar.UInt8.t -> Prims.pure_pre
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.memory_invariant", "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.l_and", "LowStar.Monotonic.Buffer.loc_disjoint", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.loc", "LowParseWriters.__proj__Mkmemory_invariant__item__lwrite", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "LowStar.Monotonic.Buffer.live", "FStar.Monotonic.HyperStack.mem", "LowParseWriters.__proj__Mkmemory_invariant__item__h0", "Prims.pure_pre" ]
[]
false
false
false
true
false
let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre =
inv.lwrite `B.loc_disjoint` (B.loc_buffer b) /\ B.live inv.h0 b
false
LowParseWriters.fsti
LowParseWriters.validate_repr
val validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t{B.len b == len}) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv)
val validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t{B.len b == len}) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv)
let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 40, "end_line": 818, "start_col": 0, "start_line": 811 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
v: LowParseWriters.LowParse.validator p -> inv: LowParseWriters.memory_invariant -> b: LowStar.Buffer.buffer FStar.UInt8.t -> len: FStar.UInt32.t{LowStar.Monotonic.Buffer.len b == len} -> LowParseWriters.read_repr (LowParseWriters.ptr p inv * FStar.UInt32.t) (LowParseWriters.validate_pre inv b) (LowParseWriters.validate_post p inv b) (LowParseWriters.validate_post_err p inv b) inv
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.LowParse.validator", "LowParseWriters.memory_invariant", "LowStar.Buffer.buffer", "FStar.UInt8.t", "FStar.UInt32.t", "Prims.eq2", "LowStar.Monotonic.Buffer.len", "LowStar.Buffer.trivial_preorder", "LowParseWriters.ReadRepr", "FStar.Pervasives.Native.tuple2", "LowParseWriters.ptr", "LowParseWriters.validate_pre", "LowParseWriters.validate_post", "LowParseWriters.validate_post_err", "LowParseWriters.validate_spec", "LowParseWriters.validate_impl", "LowParseWriters.read_repr" ]
[]
false
false
false
false
false
let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t{B.len b == len}) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) =
ReadRepr _ (validate_impl v inv b len)
false
LowParseWriters.fsti
LowParseWriters.read_return_spec
val read_return_spec (a: Type) (x: a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False))
val read_return_spec (a: Type) (x: a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False))
let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 20, "end_line": 149, "start_col": 0, "start_line": 146 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> x: a -> LowParseWriters.read_repr_spec a Prims.l_True (fun res -> res == x) (fun _ -> Prims.l_False)
Prims.Tot
[ "total" ]
[]
[ "Prims.unit", "LowParseWriters.Correct", "LowParseWriters.result", "LowParseWriters.read_repr_spec", "Prims.l_True", "Prims.eq2", "Prims.l_False" ]
[]
false
false
false
false
false
let read_return_spec (a: Type) (x: a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) =
fun _ -> Correct x
false
LowParseWriters.fsti
LowParseWriters.read_bind
val read_bind (a b: Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit]l_f: memory_invariant) (pre_g: (x: a -> pure_pre)) (post_g: (x: a -> pure_post' b (pre_g x))) (post_err_g: (x: a -> pure_post_err (pre_g x))) ([@@@ refl_implicit]l_g: memory_invariant) ([@@@ refl_implicit]pr: squash (l_f == l_g)) (f_bind: read_repr a pre_f post_f post_err_f l_f) (g: (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a). post_f x ==> pre_g x)) (fun y -> exists (x: a). pre_f /\ post_f x /\ post_g x y) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a). post_f x /\ post_err_g x ()))) l_g)
val read_bind (a b: Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit]l_f: memory_invariant) (pre_g: (x: a -> pure_pre)) (post_g: (x: a -> pure_post' b (pre_g x))) (post_err_g: (x: a -> pure_post_err (pre_g x))) ([@@@ refl_implicit]l_g: memory_invariant) ([@@@ refl_implicit]pr: squash (l_f == l_g)) (f_bind: read_repr a pre_f post_f post_err_f l_f) (g: (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a). post_f x ==> pre_g x)) (fun y -> exists (x: a). pre_f /\ post_f x /\ post_g x y) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a). post_f x /\ post_err_g x ()))) l_g)
let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) )
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 198, "end_line": 238, "start_col": 0, "start_line": 220 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> b: Type -> pre_f: Prims.pure_pre -> post_f: LowParseWriters.pure_post' a pre_f -> post_err_f: LowParseWriters.pure_post_err pre_f -> l_f: LowParseWriters.memory_invariant -> pre_g: (x: a -> Prims.pure_pre) -> post_g: (x: a -> LowParseWriters.pure_post' b (pre_g x)) -> post_err_g: (x: a -> LowParseWriters.pure_post_err (pre_g x)) -> l_g: LowParseWriters.memory_invariant -> pr: Prims.squash (l_f == l_g) -> f_bind: LowParseWriters.read_repr a pre_f post_f post_err_f l_f -> g: (x: a -> LowParseWriters.read_repr b (pre_g x) (post_g x) (post_err_g x) l_g) -> LowParseWriters.read_repr b (pre_f /\ (forall (x: a). post_f x ==> pre_g x)) (fun y -> exists (x: a). pre_f /\ post_f x /\ post_g x y) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a). post_f x /\ post_err_g x ()))) l_g
Prims.Tot
[ "total" ]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "LowParseWriters.memory_invariant", "Prims.squash", "Prims.eq2", "LowParseWriters.read_repr", "LowParseWriters.ReadRepr", "Prims.l_and", "Prims.l_Forall", "Prims.l_imp", "Prims.l_Exists", "Prims.unit", "Prims.l_or", "LowParseWriters.read_bind_spec", "LowParseWriters.__proj__ReadRepr__item__spec", "LowParseWriters.read_repr_spec", "LowParseWriters.read_bind_impl", "LowParseWriters.__proj__ReadRepr__item__impl", "LowParseWriters.read_repr_impl" ]
[]
false
false
false
false
false
let read_bind (a b: Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit]l_f: memory_invariant) (pre_g: (x: a -> pure_pre)) (post_g: (x: a -> pure_post' b (pre_g x))) (post_err_g: (x: a -> pure_post_err (pre_g x))) ([@@@ refl_implicit]l_g: memory_invariant) ([@@@ refl_implicit]pr: squash (l_f == l_g)) (f_bind: read_repr a pre_f post_f post_err_f l_f) (g: (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a). post_f x ==> pre_g x)) (fun y -> exists (x: a). pre_f /\ post_f x /\ post_g x y) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a). post_f x /\ post_err_g x ()))) l_g) =
ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)))
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.finv
val finv: res:felem -> a:felem -> Stack unit (requires fun h -> live h a /\ live h res /\ eq_or_disjoint a res /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc res) h0 h1 /\ as_nat h1 res < S.prime /\ fmont_as_nat h1 res = S.finv (fmont_as_nat h0 a))
val finv: res:felem -> a:felem -> Stack unit (requires fun h -> live h a /\ live h res /\ eq_or_disjoint a res /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc res) h0 h1 /\ as_nat h1 res < S.prime /\ fmont_as_nat h1 res = S.finv (fmont_as_nat h0 a))
let finv res a = let h0 = ST.get () in push_frame (); let tmp = create 16ul (u64 0) in let x30 = sub tmp 0ul 4ul in let x2 = sub tmp 4ul 4ul in let tmp1 = sub tmp 8ul 4ul in let tmp2 = sub tmp 12ul 4ul in finv_30 x30 x2 tmp1 tmp2 a; finv_256 tmp1 x2 x30 a; copy res tmp1; let h1 = ST.get () in assert (fmont_as_nat h1 res == SI.finv (fmont_as_nat h0 a)); SI.finv_is_finv_lemma (fmont_as_nat h0 a); pop_frame ()
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 232, "start_col": 0, "start_line": 218 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let one_mod ctx one = make_fone one inline_for_extraction noextract val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let mul_mod ctx x y xy = fmul xy x y inline_for_extraction noextract val sqr_mod : BE.lsqr_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let sqr_mod ctx x xx = fsqr xx x inline_for_extraction noextract let mk_p256_prime_concrete_ops : BE.concrete_ops U64 4ul 0ul = { BE.to = mk_to_p256_prime_comm_monoid; BE.lone = one_mod; BE.lmul = mul_mod; BE.lsqr = sqr_mod; } inline_for_extraction noextract val fsquare_times_in_place (out:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ as_nat h out < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 out) (v b)) let fsquare_times_in_place out b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 out) (v b); BE.lexp_pow2_in_place 4ul 0ul mk_p256_prime_concrete_ops (null uint64) out b inline_for_extraction noextract val fsquare_times (out a:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ live h a /\ disjoint out a /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 a) (v b)) let fsquare_times out a b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 a) (v b); BE.lexp_pow2 4ul 0ul mk_p256_prime_concrete_ops (null uint64) a b out inline_for_extraction noextract val finv_30 (x30 x2 tmp1 tmp2 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h tmp1 /\ live h tmp2 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint x30 x2 /\ disjoint x30 tmp1 /\ disjoint x30 tmp2 /\ disjoint x2 tmp1 /\ disjoint x2 tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc x30 |+| loc x2 |+| loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 x30 < S.prime /\ as_nat h1 x2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2_s = S.fmul (SI.fsquare_times f 1) f in let x3_s = S.fmul (SI.fsquare_times x2_s 1) f in let x6_s = S.fmul (SI.fsquare_times x3_s 3) x3_s in let x12_s = S.fmul (SI.fsquare_times x6_s 6) x6_s in let x15_s = S.fmul (SI.fsquare_times x12_s 3) x3_s in let x30_s = S.fmul (SI.fsquare_times x15_s 15) x15_s in fmont_as_nat h1 x30 = x30_s /\ fmont_as_nat h1 x2 = x2_s)) let finv_30 x30 x2 tmp1 tmp2 a = let h0 = ST.get () in fsquare_times x2 a 1ul; fmul x2 x2 a; let h1 = ST.get () in assert (fmont_as_nat h1 x2 == S.fmul (SI.fsquare_times (fmont_as_nat h0 a) 1) (fmont_as_nat h0 a)); fsquare_times x30 x2 1ul; fmul x30 x30 a; let h2 = ST.get () in assert (fmont_as_nat h2 x30 == // x3 S.fmul (SI.fsquare_times (fmont_as_nat h1 x2) 1) (fmont_as_nat h0 a)); fsquare_times tmp1 x30 3ul; fmul tmp1 tmp1 x30; let h3 = ST.get () in assert (fmont_as_nat h3 tmp1 == // x6 S.fmul (SI.fsquare_times (fmont_as_nat h2 x30) 3) (fmont_as_nat h2 x30)); fsquare_times tmp2 tmp1 6ul; fmul tmp2 tmp2 tmp1; let h4 = ST.get () in assert (fmont_as_nat h4 tmp2 == // x12 S.fmul (SI.fsquare_times (fmont_as_nat h3 tmp1) 6) (fmont_as_nat h3 tmp1)); fsquare_times tmp1 tmp2 3ul; fmul tmp1 tmp1 x30; let h5 = ST.get () in assert (fmont_as_nat h5 tmp1 == // x15 S.fmul (SI.fsquare_times (fmont_as_nat h4 tmp2) 3) (fmont_as_nat h2 x30)); fsquare_times x30 tmp1 15ul; fmul x30 x30 tmp1; let h6 = ST.get () in assert (fmont_as_nat h6 x30 == // x30 S.fmul (SI.fsquare_times (fmont_as_nat h5 tmp1) 15) (fmont_as_nat h5 tmp1)) inline_for_extraction noextract val finv_256 (x256 x2 x30 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h x256 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a x256 /\ disjoint x30 x2 /\ disjoint x30 x256 /\ disjoint x2 x256 /\ as_nat h a < S.prime /\ as_nat h x30 < S.prime /\ as_nat h x2 < S.prime) (ensures fun h0 _ h1 -> modifies (loc x256 |+| loc x2) h0 h1 /\ as_nat h1 x256 < S.prime /\ (let f = fmont_as_nat h0 a in let x30 = fmont_as_nat h0 x30 in let x2 = fmont_as_nat h0 x2 in let x32_s = S.fmul (SI.fsquare_times x30 2) x2 in let x64_s = S.fmul (SI.fsquare_times x32_s 32) f in let x192_s = S.fmul (SI.fsquare_times x64_s 128) x32_s in let x224_s = S.fmul (SI.fsquare_times x192_s 32) x32_s in let x254_s = S.fmul (SI.fsquare_times x224_s 30) x30 in let x256_s = S.fmul (SI.fsquare_times x254_s 2) f in fmont_as_nat h1 x256 = x256_s)) let finv_256 x256 x2 x30 a = let h0 = ST.get () in fsquare_times x256 x30 2ul; fmul x256 x256 x2; let h1 = ST.get () in assert (fmont_as_nat h1 x256 == // x32 S.fmul (SI.fsquare_times (fmont_as_nat h0 x30) 2) (fmont_as_nat h0 x2)); fsquare_times x2 x256 32ul; fmul x2 x2 a; let h2 = ST.get () in assert (fmont_as_nat h2 x2 == // x64 S.fmul (SI.fsquare_times (fmont_as_nat h1 x256) 32) (fmont_as_nat h0 a)); fsquare_times_in_place x2 128ul; fmul x2 x2 x256; let h3 = ST.get () in assert (fmont_as_nat h3 x2 == // x192 S.fmul (SI.fsquare_times (fmont_as_nat h2 x2) 128) (fmont_as_nat h1 x256)); fsquare_times_in_place x2 32ul; fmul x2 x2 x256; let h4 = ST.get () in assert (fmont_as_nat h4 x2 == // x224 S.fmul (SI.fsquare_times (fmont_as_nat h3 x2) 32) (fmont_as_nat h1 x256)); fsquare_times_in_place x2 30ul; fmul x2 x2 x30; let h5 = ST.get () in assert (fmont_as_nat h5 x2 == // x254 S.fmul (SI.fsquare_times (fmont_as_nat h4 x2) 30) (fmont_as_nat h0 x30)); fsquare_times_in_place x2 2ul; fmul x256 x2 a; let h6 = ST.get () in assert (fmont_as_nat h6 x256 == // x256 S.fmul (SI.fsquare_times (fmont_as_nat h5 x2) 2) (fmont_as_nat h0 a))
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
res: Hacl.Impl.P256.Bignum.felem -> a: Hacl.Impl.P256.Bignum.felem -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Impl.P256.Bignum.felem", "FStar.HyperStack.ST.pop_frame", "Prims.unit", "Hacl.Spec.P256.Finv.finv_is_finv_lemma", "Hacl.Impl.P256.Field.fmont_as_nat", "Prims._assert", "Prims.eq2", "Spec.P256.PointOps.felem", "Hacl.Spec.P256.Finv.finv", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "Lib.Buffer.copy", "Lib.Buffer.MUT", "Lib.IntTypes.uint64", "Lib.IntTypes.size", "Hacl.Impl.P256.Finv.finv_256", "Hacl.Impl.P256.Finv.finv_30", "Lib.Buffer.lbuffer_t", "Lib.IntTypes.int_t", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "FStar.UInt32.uint_to_t", "FStar.UInt32.t", "Lib.Buffer.sub", "FStar.UInt32.__uint_to_t", "Lib.Buffer.create", "Lib.IntTypes.u64", "Lib.Buffer.lbuffer", "FStar.HyperStack.ST.push_frame" ]
[]
false
true
false
false
false
let finv res a =
let h0 = ST.get () in push_frame (); let tmp = create 16ul (u64 0) in let x30 = sub tmp 0ul 4ul in let x2 = sub tmp 4ul 4ul in let tmp1 = sub tmp 8ul 4ul in let tmp2 = sub tmp 12ul 4ul in finv_30 x30 x2 tmp1 tmp2 a; finv_256 tmp1 x2 x30 a; copy res tmp1; let h1 = ST.get () in assert (fmont_as_nat h1 res == SI.finv (fmont_as_nat h0 a)); SI.finv_is_finv_lemma (fmont_as_nat h0 a); pop_frame ()
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.change_equal_slprop
val change_equal_slprop (#opened:inames) (p q: vprop) : SteelGhost unit opened p (fun _ -> q) (fun _ -> p == q) (fun h0 _ h1 -> p == q /\ h1 q == h0 p)
val change_equal_slprop (#opened:inames) (p q: vprop) : SteelGhost unit opened p (fun _ -> q) (fun _ -> p == q) (fun h0 _ h1 -> p == q /\ h1 q == h0 p)
let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ())
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 17, "end_line": 438, "start_col": 0, "start_line": 428 }
(* 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.Effect.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Steel.Effect.Common.vprop -> q: Steel.Effect.Common.vprop -> Steel.Effect.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Atomic.change_slprop", "Steel.Memory.mem", "Prims.unit", "FStar.Ghost.erased", "Steel.Effect.Common.t_of", "FStar.Ghost.hide", "FStar.Ghost.reveal", "Steel.Effect.Common.rmem", "Steel.Effect.Common.rmem'", "Steel.Effect.Common.valid_rmem", "Steel.Effect.Atomic.get" ]
[]
false
true
false
false
false
let change_equal_slprop p q =
let m = get () in let x:Ghost.erased (t_of p) = hide ((reveal m) p) in let y:Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ())
false
LowParseWriters.fsti
LowParseWriters.read_subcomp
val read_subcomp (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l': memory_invariant) (f_subcomp: read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True))
val read_subcomp (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l': memory_invariant) (f_subcomp: read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True))
let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () )
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 293, "start_col": 0, "start_line": 282 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> pre: Prims.pure_pre -> post: LowParseWriters.pure_post' a pre -> post_err: LowParseWriters.pure_post_err pre -> l: LowParseWriters.memory_invariant -> pre': Prims.pure_pre -> post': LowParseWriters.pure_post' a pre' -> post_err': LowParseWriters.pure_post_err pre' -> l': LowParseWriters.memory_invariant -> f_subcomp: LowParseWriters.read_repr a pre post post_err l -> Prims.Pure (LowParseWriters.read_repr a pre' post' post_err' l')
Prims.Pure
[]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "LowParseWriters.memory_invariant", "LowParseWriters.read_repr", "LowParseWriters.ReadRepr", "LowParseWriters.read_subcomp_spec", "LowParseWriters.__proj__ReadRepr__item__spec", "LowParseWriters.read_subcomp_impl", "LowParseWriters.__proj__ReadRepr__item__impl", "LowParseWriters.read_subcomp_cond", "Prims.l_True" ]
[]
false
false
false
false
false
let read_subcomp (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l': memory_invariant) (f_subcomp: read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) =
ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) ())
false
Steel.Channel.Simplex.fst
Steel.Channel.Simplex.recv
val recv (#p:prot) (#next:prot{more next}) (c:chan p) : SteelT (msg_t next) (receiver c next) (fun x -> receiver c (step next x))
val recv (#p:prot) (#next:prot{more next}) (c:chan p) : SteelT (msg_t next) (receiver c next) (fun x -> receiver c (step next x))
let rec recv (#p:prot) (#next:prot{more next}) (c:chan p) : SteelT (msg_t next) (receiver c next) (fun x -> receiver c (step next x)) = let v = send_receive_prelude c in if (fst v).chan_ctr = (snd v).chan_ctr then ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (pure (fst v == snd v)) (fun _ -> ()); elim_pure (fst v == snd v); intro_chan_inv_eqT c.chan_chan (fst v) (snd v); Steel.SpinLock.release c.chan_lock; recv c ) else ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (chan_inv_step (snd v) (fst v)) (fun _ -> ()); recv_availableT c (fst v) (snd v) () )
{ "file_name": "lib/steel/Steel.Channel.Simplex.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 5, "end_line": 408, "start_col": 0, "start_line": 390 }
(* 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.Channel.Simplex module P = Steel.Channel.Protocol open Steel.SpinLock open Steel.Memory open Steel.Effect.Atomic open Steel.Effect open Steel.HigherReference open Steel.FractionalPermission module MRef = Steel.MonotonicHigherReference module H = Steel.HigherReference let sprot = p:prot { more p } noeq type chan_val = { chan_prot : sprot; chan_msg : msg_t chan_prot; chan_ctr : nat } let mref a p = MRef.ref a p let trace_ref (p:prot) = mref (partial_trace_of p) extended_to noeq type chan_t (p:prot) = { send: ref chan_val; recv: ref chan_val; trace: trace_ref p; } let half : perm = half_perm full_perm let step (s:sprot) (x:msg_t s) = step s x let chan_inv_step_p (vrecv vsend:chan_val) : prop = (vsend.chan_prot == step vrecv.chan_prot vrecv.chan_msg /\ vsend.chan_ctr == vrecv.chan_ctr + 1) let chan_inv_step (vrecv vsend:chan_val) : vprop = pure (chan_inv_step_p vrecv vsend) let chan_inv_cond (vsend:chan_val) (vrecv:chan_val) : vprop = if vsend.chan_ctr = vrecv.chan_ctr then pure (vsend == vrecv) else chan_inv_step vrecv vsend let trace_until_prop #p (r:trace_ref p) (vr:chan_val) (tr: partial_trace_of p) : vprop = MRef.pts_to r full_perm tr `star` pure (until tr == step vr.chan_prot vr.chan_msg) let trace_until #p (r:trace_ref p) (vr:chan_val) = h_exists (trace_until_prop r vr) let chan_inv_recv #p (c:chan_t p) (vsend:chan_val) = h_exists (fun (vrecv:chan_val) -> pts_to c.recv half vrecv `star` trace_until c.trace vrecv `star` chan_inv_cond vsend vrecv) let chan_inv #p (c:chan_t p) : vprop = h_exists (fun (vsend:chan_val) -> pts_to c.send half vsend `star` chan_inv_recv c vsend) let intro_chan_inv_cond_eqT (vs vr:chan_val) : Steel unit emp (fun _ -> chan_inv_cond vs vr) (requires fun _ -> vs == vr) (ensures fun _ _ _ -> True) = intro_pure (vs == vs); rewrite_slprop (chan_inv_cond vs vs) (chan_inv_cond vs vr) (fun _ -> ()) let intro_chan_inv_cond_stepT (vs vr:chan_val) : SteelT unit (chan_inv_step vr vs) (fun _ -> chan_inv_cond vs vr) = Steel.Utils.extract_pure (chan_inv_step_p vr vs); rewrite_slprop (chan_inv_step vr vs) (chan_inv_cond vs vr) (fun _ -> ()) let intro_chan_inv_auxT #p (#vs : chan_val) (#vr : chan_val) (c:chan_t p) : SteelT unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_cond vs vr) (fun _ -> chan_inv c) = intro_exists _ (fun (vr:chan_val) -> pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_cond vs vr); intro_exists _ (fun (vs:chan_val) -> pts_to c.send half vs `star` chan_inv_recv c vs) let intro_chan_inv_stepT #p (c:chan_t p) (vs vr:chan_val) : SteelT unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_step vr vs) (fun _ -> chan_inv c) = intro_chan_inv_cond_stepT vs vr; intro_chan_inv_auxT c let intro_chan_inv_eqT #p (c:chan_t p) (vs vr:chan_val) : Steel unit (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr) (fun _ -> chan_inv c) (requires fun _ -> vs == vr) (ensures fun _ _ _ -> True) = intro_chan_inv_cond_eqT vs vr; intro_chan_inv_auxT c noeq type chan p = { chan_chan : chan_t p; chan_lock : lock (chan_inv chan_chan) } let in_state_prop (p:prot) (vsend:chan_val) : prop = p == step vsend.chan_prot vsend.chan_msg irreducible let next_chan_val (#p:sprot) (x:msg_t p) (vs0:chan_val { in_state_prop p vs0 }) : Tot (vs:chan_val{in_state_prop (step p x) vs /\ chan_inv_step_p vs0 vs}) = { chan_prot = (step vs0.chan_prot vs0.chan_msg); chan_msg = x; chan_ctr = vs0.chan_ctr + 1 } [@@__reduce__] let in_state_slprop (p:prot) (vsend:chan_val) : vprop = pure (in_state_prop p vsend) let in_state (r:ref chan_val) (p:prot) = h_exists (fun (vsend:chan_val) -> pts_to r half vsend `star` in_state_slprop p vsend) let sender #q (c:chan q) (p:prot) = in_state c.chan_chan.send p let receiver #q (c:chan q) (p:prot) = in_state c.chan_chan.recv p let intro_chan_inv #p (c:chan_t p) (v:chan_val) : SteelT unit (pts_to c.send half v `star` pts_to c.recv half v `star` trace_until c.trace v) (fun _ -> chan_inv c) = intro_chan_inv_eqT c v v let chan_val_p (p:prot) = (vs0:chan_val { in_state_prop p vs0 }) let intro_in_state (r:ref chan_val) (p:prot) (v:chan_val_p p) : SteelT unit (pts_to r half v) (fun _ -> in_state r p) = intro_pure (in_state_prop p v); intro_exists v (fun (v:chan_val) -> pts_to r half v `star` in_state_slprop p v) let msg t p = Msg Send unit (fun _ -> p) let init_chan_val (p:prot) = v:chan_val {v.chan_prot == msg unit p} let initial_trace (p:prot) : (q:partial_trace_of p {until q == p}) = { to = p; tr=Waiting p} let intro_trace_until #q (r:trace_ref q) (tr:partial_trace_of q) (v:chan_val) : Steel unit (MRef.pts_to r full_perm tr) (fun _ -> trace_until r v) (requires fun _ -> until tr == step v.chan_prot v.chan_msg) (ensures fun _ _ _ -> True) = intro_pure (until tr == step v.chan_prot v.chan_msg); intro_exists tr (fun (tr:partial_trace_of q) -> MRef.pts_to r full_perm tr `star` pure (until tr == (step v.chan_prot v.chan_msg))); () let chan_t_sr (p:prot) (send recv:ref chan_val) = (c:chan_t p{c.send == send /\ c.recv == recv}) let intro_trace_until_init #p (c:chan_t p) (v:init_chan_val p) : SteelT unit (MRef.pts_to c.trace full_perm (initial_trace p)) (fun _ -> trace_until c.trace v) = intro_pure (until (initial_trace p) == step v.chan_prot v.chan_msg); //TODO: Not sure why I need this rewrite rewrite_slprop (MRef.pts_to c.trace full_perm (initial_trace p) `star` pure (until (initial_trace p) == step v.chan_prot v.chan_msg)) (MRef.pts_to c.trace full_perm (initial_trace p) `star` pure (until (initial_trace p) == step v.chan_prot v.chan_msg)) (fun _ -> ()); intro_exists (initial_trace p) (trace_until_prop c.trace v) let mk_chan (#p:prot) (send recv:ref chan_val) (v:init_chan_val p) : SteelT (chan_t_sr p send recv) (pts_to send half v `star` pts_to recv half v) (fun c -> chan_inv c) = let tr: trace_ref p = MRef.alloc (extended_to #p) (initial_trace p) in let c = Mkchan_t send recv tr in rewrite_slprop (MRef.pts_to tr full_perm (initial_trace p)) (MRef.pts_to c.trace full_perm (initial_trace p)) (fun _ -> ()); intro_trace_until_init c v; rewrite_slprop (pts_to send half v `star` pts_to recv half v) (pts_to c.send half v `star` pts_to c.recv half v) (fun _ -> ()); intro_chan_inv #p c v; let c' : chan_t_sr p send recv = c in rewrite_slprop (chan_inv c) (chan_inv c') (fun _ -> ()); return c' let new_chan (p:prot) : SteelT (chan p) emp (fun c -> sender c p `star` receiver c p) = let q = msg unit p in let v : chan_val = { chan_prot = q; chan_msg = (); chan_ctr = 0 } in let vp : init_chan_val p = v in let send = H.alloc v in let recv = H.alloc v in H.share recv; H.share send; (* TODO: use smt_fallback *) rewrite_slprop (pts_to send (half_perm full_perm) v `star` pts_to send (half_perm full_perm) v `star` pts_to recv (half_perm full_perm) v `star` pts_to recv (half_perm full_perm) v) (pts_to send half vp `star` pts_to send half vp `star` pts_to recv half vp `star` pts_to recv half vp) (fun _ -> ()); let c = mk_chan send recv vp in intro_in_state send p vp; intro_in_state recv p vp; let l = Steel.SpinLock.new_lock (chan_inv c) in let ch = { chan_chan = c; chan_lock = l } in rewrite_slprop (in_state send p) (sender ch p) (fun _ -> ()); rewrite_slprop (in_state recv p) (receiver ch p) (fun _ -> ()); return ch [@@__reduce__] let send_recv_in_sync (r:ref chan_val) (p:prot{more p}) #q (c:chan_t q) (vs vr:chan_val) : vprop = (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` pure (vs == vr) `star` in_state r p) [@@__reduce__] let sender_ahead (r:ref chan_val) (p:prot{more p}) #q (c:chan_t q) (vs vr:chan_val) : vprop = (pts_to c.send half vs `star` pts_to c.recv half vr `star` trace_until c.trace vr `star` chan_inv_step vr vs `star` in_state r p) let update_channel (#p:sprot) #q (c:chan_t q) (x:msg_t p) (vs:chan_val) (r:ref chan_val) : SteelT chan_val (pts_to r full_perm vs `star` in_state_slprop p vs) (fun vs' -> pts_to r full_perm vs' `star` (in_state_slprop (step p x) vs' `star` chan_inv_step vs vs')) = elim_pure (in_state_prop p vs); let vs' = next_chan_val x vs in H.write r vs'; intro_pure (in_state_prop (step p x) vs'); intro_pure (chan_inv_step_p vs vs'); return vs' [@@__reduce__] let send_pre_available (p:sprot) #q (c:chan_t q) (vs vr:chan_val) = send_recv_in_sync c.send p c vs vr let gather_r (#p:sprot) (r:ref chan_val) (v:chan_val) : SteelT unit (pts_to r half v `star` in_state r p) (fun _ -> pts_to r full_perm v `star` in_state_slprop p v) = let v' = witness_exists () in H.higher_ref_pts_to_injective_eq #_ #_ #_ #_ #v #_ r; H.gather #_ #_ #half #half #v #v r; rewrite_slprop (pts_to r (sum_perm half half) v) (pts_to r full_perm v) (fun _ -> ()); rewrite_slprop (in_state_slprop p v') (in_state_slprop p v) (fun _ -> ()) let send_available (#p:sprot) #q (cc:chan q) (x:msg_t p) (vs vr:chan_val) (_:unit) : SteelT unit (send_pre_available p #q cc.chan_chan vs vr) (fun _ -> sender cc (step p x)) = Steel.Utils.extract_pure (vs == vr); Steel.Utils.rewrite #_ #(send_recv_in_sync cc.chan_chan.send p cc.chan_chan vs) vr vs; elim_pure (vs == vs); gather_r cc.chan_chan.send vs; let next_vs = update_channel cc.chan_chan x vs cc.chan_chan.send in H.share cc.chan_chan.send; intro_exists next_vs (fun (next_vs:chan_val) -> pts_to cc.chan_chan.send half next_vs `star` in_state_slprop (step p x) next_vs); intro_chan_inv_stepT cc.chan_chan next_vs vs; Steel.SpinLock.release cc.chan_lock let extensible (#p:prot) (x:partial_trace_of p) = P.more x.to let next_msg_t (#p:prot) (x:partial_trace_of p) = P.next_msg_t x.to let next_trace #p (vr:chan_val) (vs:chan_val) (tr:partial_trace_of p) (s:squash (until tr == step vr.chan_prot vr.chan_msg)) (_:squash (chan_inv_step_p vr vs)) : (ts:partial_trace_of p { until ts == step vs.chan_prot vs.chan_msg }) = let msg : next_msg_t tr = vs.chan_msg in assert (extensible tr); extend_partial_trace tr msg let next_trace_st #p (vr:chan_val) (vs:chan_val) (tr:partial_trace_of p) : Steel (extension_of tr) (chan_inv_step vr vs) (fun _ -> emp) (requires fun _ -> until tr == step vr.chan_prot vr.chan_msg) (ensures fun _ ts _ -> until ts == step vs.chan_prot vs.chan_msg) = elim_pure (chan_inv_step_p vr vs); let ts : extension_of tr = next_trace vr vs tr () () in return ts let update_trace #p (r:trace_ref p) (vr:chan_val) (vs:chan_val) : Steel unit (trace_until r vr) (fun _ -> trace_until r vs) (requires fun _ -> chan_inv_step_p vr vs) (ensures fun _ _ _ -> True) = intro_pure (chan_inv_step_p vr vs); let tr = MRef.read_refine r in elim_pure (until tr == step vr.chan_prot vr.chan_msg); let ts : extension_of tr = next_trace_st vr vs tr in MRef.write r ts; intro_pure (until ts == step vs.chan_prot vs.chan_msg); intro_exists ts (fun (ts:partial_trace_of p) -> MRef.pts_to r full_perm ts `star` pure (until ts == step vs.chan_prot vs.chan_msg)) let recv_availableT (#p:sprot) #q (cc:chan q) (vs vr:chan_val) (_:unit) : SteelT (msg_t p) (sender_ahead cc.chan_chan.recv p cc.chan_chan vs vr) (fun x -> receiver cc (step p x)) = elim_pure (chan_inv_step_p vr vs); gather_r cc.chan_chan.recv vr; elim_pure (in_state_prop p vr); H.write cc.chan_chan.recv vs; H.share cc.chan_chan.recv; assert (vs.chan_prot == p); let vs_msg : msg_t p = vs.chan_msg in intro_pure (in_state_prop (step p vs_msg) vs); intro_exists vs (fun (vs:chan_val) -> pts_to cc.chan_chan.recv half vs `star` in_state_slprop (step p vs_msg) vs); update_trace cc.chan_chan.trace vr vs; intro_chan_inv cc.chan_chan vs; Steel.SpinLock.release cc.chan_lock; vs_msg #push-options "--ide_id_info_off" let send_receive_prelude (#p:prot) (cc:chan p) : SteelT (chan_val & chan_val) emp (fun v -> pts_to cc.chan_chan.send half (fst v) `star` pts_to cc.chan_chan.recv half (snd v) `star` trace_until cc.chan_chan.trace (snd v) `star` chan_inv_cond (fst v) (snd v)) = let c = cc.chan_chan in Steel.SpinLock.acquire cc.chan_lock; let vs = read_refine (chan_inv_recv cc.chan_chan) cc.chan_chan.send in let _ = witness_exists () in let vr = H.read cc.chan_chan.recv in rewrite_slprop (trace_until _ _ `star` chan_inv_cond _ _) (trace_until cc.chan_chan.trace vr `star` chan_inv_cond vs vr) (fun _ -> ()); return (vs, vr) let rec send (#p:prot) (c:chan p) (#next:prot{more next}) (x:msg_t next) : SteelT unit (sender c next) (fun _ -> sender c (step next x)) = let v = send_receive_prelude c in //matching v as vs,vr fails if (fst v).chan_ctr = (snd v).chan_ctr then ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (pure (fst v == snd v)) (fun _ -> ()); send_available c x (fst v) (snd v) () //TODO: inlining send_availableT here fails ) else ( rewrite_slprop (chan_inv_cond (fst v) (snd v)) (chan_inv_step (snd v) (fst v)) (fun _ -> ()); intro_chan_inv_stepT c.chan_chan (fst v) (snd v); Steel.SpinLock.release c.chan_lock; send c x )
{ "checked_file": "/", "dependencies": [ "Steel.Utils.fst.checked", "Steel.SpinLock.fsti.checked", "Steel.MonotonicHigherReference.fsti.checked", "Steel.Memory.fsti.checked", "Steel.HigherReference.fsti.checked", "Steel.FractionalPermission.fst.checked", "Steel.Effect.Atomic.fsti.checked", "Steel.Effect.fsti.checked", "Steel.Channel.Protocol.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Steel.Channel.Simplex.fst" }
[ { "abbrev": true, "full_module": "Steel.HigherReference", "short_module": "H" }, { "abbrev": true, "full_module": "Steel.MonotonicHigherReference", "short_module": "MRef" }, { "abbrev": false, "full_module": "Steel.FractionalPermission", "short_module": null }, { "abbrev": false, "full_module": "Steel.HigherReference", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Atomic", "short_module": null }, { "abbrev": false, "full_module": "Steel.SpinLock", "short_module": null }, { "abbrev": true, "full_module": "Steel.Channel.Protocol", "short_module": "P" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Memory", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel.Protocol", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel", "short_module": null }, { "abbrev": false, "full_module": "Steel.Channel", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
c: Steel.Channel.Simplex.chan p -> Steel.Effect.SteelT (Steel.Channel.Protocol.msg_t next)
Steel.Effect.SteelT
[]
[]
[ "Steel.Channel.Simplex.prot", "Prims.b2t", "Steel.Channel.Protocol.more", "Steel.Channel.Simplex.chan", "Prims.op_Equality", "Prims.nat", "Steel.Channel.Simplex.__proj__Mkchan_val__item__chan_ctr", "FStar.Pervasives.Native.fst", "Steel.Channel.Simplex.chan_val", "FStar.Pervasives.Native.snd", "Steel.Channel.Simplex.recv", "Steel.Channel.Protocol.msg_t", "Prims.unit", "Steel.SpinLock.release", "Steel.Channel.Simplex.chan_inv", "Steel.Channel.Simplex.__proj__Mkchan__item__chan_chan", "Steel.Channel.Simplex.__proj__Mkchan__item__chan_lock", "Steel.Channel.Simplex.intro_chan_inv_eqT", "Steel.Effect.Atomic.elim_pure", "FStar.Ghost.hide", "FStar.Set.set", "Steel.Memory.iname", "FStar.Set.empty", "Prims.eq2", "Steel.Effect.Atomic.rewrite_slprop", "Steel.Channel.Simplex.chan_inv_cond", "Steel.Effect.Common.pure", "Steel.Memory.mem", "Prims.bool", "Steel.Channel.Simplex.recv_availableT", "Steel.Channel.Simplex.chan_inv_step", "FStar.Pervasives.Native.tuple2", "Steel.Channel.Simplex.send_receive_prelude", "Steel.Channel.Simplex.receiver", "Steel.Channel.Simplex.step", "Steel.Effect.Common.vprop" ]
[ "recursion" ]
false
true
false
false
false
let rec recv (#p: prot) (#next: prot{more next}) (c: chan p) : SteelT (msg_t next) (receiver c next) (fun x -> receiver c (step next x)) =
let v = send_receive_prelude c in if (fst v).chan_ctr = (snd v).chan_ctr then (rewrite_slprop (chan_inv_cond (fst v) (snd v)) (pure (fst v == snd v)) (fun _ -> ()); elim_pure (fst v == snd v); intro_chan_inv_eqT c.chan_chan (fst v) (snd v); Steel.SpinLock.release c.chan_lock; recv c) else (rewrite_slprop (chan_inv_cond (fst v) (snd v)) (chan_inv_step (snd v) (fst v)) (fun _ -> ()); recv_availableT c (fst v) (snd v) ())
false
LowParseWriters.fsti
LowParseWriters.buffer_offset
val buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0
val buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0
let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b }
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 42, "end_line": 909, "start_col": 0, "start_line": 905 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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: LowStar.Buffer.buffer t -> Type0
Prims.Tot
[ "total" ]
[]
[ "LowStar.Buffer.buffer", "FStar.UInt32.t", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder" ]
[]
false
false
false
true
true
let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 =
pos1: U32.t{U32.v pos1 <= B.length b}
false
LowParseWriters.fsti
LowParseWriters.try_rewrite_equality
val try_rewrite_equality (x: term) (bs: binders) : Tac unit
val try_rewrite_equality (x: term) (bs: binders) : Tac unit
let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 11, "end_line": 453, "start_col": 0, "start_line": 442 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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.Tactics.NamedView.term -> bs: FStar.Tactics.NamedView.binders -> FStar.Tactics.Effect.Tac Prims.unit
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Tactics.NamedView.term", "FStar.Tactics.NamedView.binders", "Prims.unit", "FStar.Tactics.NamedView.binder", "Prims.list", "FStar.Pervasives.Native.option", "FStar.Stubs.Reflection.Types.typ", "FStar.Stubs.Reflection.V2.Builtins.term_eq", "FStar.Stubs.Tactics.V2.Builtins.rewrite", "FStar.Tactics.NamedView.binder_to_binding", "Prims.bool", "LowParseWriters.try_rewrite_equality", "FStar.Reflection.V2.Formula.formula", "FStar.Reflection.V2.Formula.term_as_formula'", "FStar.Tactics.NamedView.__proj__Mkbinder__item__sort" ]
[ "recursion" ]
false
true
false
false
false
let rec try_rewrite_equality (x: term) (bs: binders) : Tac unit =
match bs with | [] -> () | x_t :: bs -> match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs
false
LowParseWriters.fsti
LowParseWriters.lift_pure_read_spec
val lift_pure_read_spec (a: Type) (wp: pure_wp a) (f_pure_spec: (unit -> PURE a wp)) : Tot (read_repr_spec a (wp (fun _ -> True)) (fun x -> ~(wp (fun x' -> ~(x == x')))) (fun _ -> False))
val lift_pure_read_spec (a: Type) (wp: pure_wp a) (f_pure_spec: (unit -> PURE a wp)) : Tot (read_repr_spec a (wp (fun _ -> True)) (fun x -> ~(wp (fun x' -> ~(x == x')))) (fun _ -> False))
let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ())
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 36, "end_line": 341, "start_col": 0, "start_line": 333 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> wp: Prims.pure_wp a -> f_pure_spec: (_: Prims.unit -> Prims.PURE a) -> LowParseWriters.read_repr_spec a (wp (fun _ -> Prims.l_True)) (fun x -> ~(wp (fun x' -> ~(x == x')))) (fun _ -> Prims.l_False)
Prims.Tot
[ "total" ]
[]
[ "Prims.pure_wp", "Prims.unit", "LowParseWriters.Correct", "LowParseWriters.result", "FStar.Monotonic.Pure.elim_pure_wp_monotonicity", "LowParseWriters.read_repr_spec", "Prims.l_True", "Prims.l_not", "Prims.eq2", "Prims.l_False" ]
[]
false
false
false
false
false
let lift_pure_read_spec (a: Type) (wp: pure_wp a) (f_pure_spec: (unit -> PURE a wp)) : Tot (read_repr_spec a (wp (fun _ -> True)) (fun x -> ~(wp (fun x' -> ~(x == x')))) (fun _ -> False)) =
FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ())
false
LowParseWriters.fsti
LowParseWriters.reify_read
val reify_read (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: (unit -> ERead a pre post post_err l)) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ (HS.get_tip l.h0) `HS.includes` (HS.get_tip h) /\ pre)) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r ()))
val reify_read (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: (unit -> ERead a pre post post_err l)) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ (HS.get_tip l.h0) `HS.includes` (HS.get_tip h) /\ pre)) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r ()))
let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 71, "end_line": 407, "start_col": 0, "start_line": 389 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ()))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> pre: Prims.pure_pre -> post: LowParseWriters.pure_post' a pre -> post_err: LowParseWriters.pure_post_err pre -> l: LowParseWriters.memory_invariant -> $r: (_: Prims.unit -> LowParseWriters.ERead a) -> FStar.HyperStack.ST.Stack (LowParseWriters.result a)
FStar.HyperStack.ST.Stack
[]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.extract_read_repr_impl", "LowParseWriters.destr_read_repr_spec", "LowParseWriters.destr_read_repr_impl", "LowParseWriters.result", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.loc", "LowParseWriters.__proj__Mkmemory_invariant__item__lwrite", "LowParseWriters.__proj__Mkmemory_invariant__item__h0", "Prims.b2t", "FStar.Monotonic.HyperHeap.includes", "FStar.Monotonic.HyperStack.get_tip", "LowStar.Monotonic.Buffer.loc_none", "Prims.eq2" ]
[]
false
true
false
false
false
let reify_read (a: Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: (unit -> ERead a pre post post_err l)) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ (HS.get_tip l.h0) `HS.includes` (HS.get_tip h) /\ pre)) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r ())) =
extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r)
false
Hacl.Impl.P256.Finv.fst
Hacl.Impl.P256.Finv.finv_30
val finv_30 (x30 x2 tmp1 tmp2 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h tmp1 /\ live h tmp2 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint x30 x2 /\ disjoint x30 tmp1 /\ disjoint x30 tmp2 /\ disjoint x2 tmp1 /\ disjoint x2 tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc x30 |+| loc x2 |+| loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 x30 < S.prime /\ as_nat h1 x2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2_s = S.fmul (SI.fsquare_times f 1) f in let x3_s = S.fmul (SI.fsquare_times x2_s 1) f in let x6_s = S.fmul (SI.fsquare_times x3_s 3) x3_s in let x12_s = S.fmul (SI.fsquare_times x6_s 6) x6_s in let x15_s = S.fmul (SI.fsquare_times x12_s 3) x3_s in let x30_s = S.fmul (SI.fsquare_times x15_s 15) x15_s in fmont_as_nat h1 x30 = x30_s /\ fmont_as_nat h1 x2 = x2_s))
val finv_30 (x30 x2 tmp1 tmp2 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h tmp1 /\ live h tmp2 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint x30 x2 /\ disjoint x30 tmp1 /\ disjoint x30 tmp2 /\ disjoint x2 tmp1 /\ disjoint x2 tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc x30 |+| loc x2 |+| loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 x30 < S.prime /\ as_nat h1 x2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2_s = S.fmul (SI.fsquare_times f 1) f in let x3_s = S.fmul (SI.fsquare_times x2_s 1) f in let x6_s = S.fmul (SI.fsquare_times x3_s 3) x3_s in let x12_s = S.fmul (SI.fsquare_times x6_s 6) x6_s in let x15_s = S.fmul (SI.fsquare_times x12_s 3) x3_s in let x30_s = S.fmul (SI.fsquare_times x15_s 15) x15_s in fmont_as_nat h1 x30 = x30_s /\ fmont_as_nat h1 x2 = x2_s))
let finv_30 x30 x2 tmp1 tmp2 a = let h0 = ST.get () in fsquare_times x2 a 1ul; fmul x2 x2 a; let h1 = ST.get () in assert (fmont_as_nat h1 x2 == S.fmul (SI.fsquare_times (fmont_as_nat h0 a) 1) (fmont_as_nat h0 a)); fsquare_times x30 x2 1ul; fmul x30 x30 a; let h2 = ST.get () in assert (fmont_as_nat h2 x30 == // x3 S.fmul (SI.fsquare_times (fmont_as_nat h1 x2) 1) (fmont_as_nat h0 a)); fsquare_times tmp1 x30 3ul; fmul tmp1 tmp1 x30; let h3 = ST.get () in assert (fmont_as_nat h3 tmp1 == // x6 S.fmul (SI.fsquare_times (fmont_as_nat h2 x30) 3) (fmont_as_nat h2 x30)); fsquare_times tmp2 tmp1 6ul; fmul tmp2 tmp2 tmp1; let h4 = ST.get () in assert (fmont_as_nat h4 tmp2 == // x12 S.fmul (SI.fsquare_times (fmont_as_nat h3 tmp1) 6) (fmont_as_nat h3 tmp1)); fsquare_times tmp1 tmp2 3ul; fmul tmp1 tmp1 x30; let h5 = ST.get () in assert (fmont_as_nat h5 tmp1 == // x15 S.fmul (SI.fsquare_times (fmont_as_nat h4 tmp2) 3) (fmont_as_nat h2 x30)); fsquare_times x30 tmp1 15ul; fmul x30 x30 tmp1; let h6 = ST.get () in assert (fmont_as_nat h6 x30 == // x30 S.fmul (SI.fsquare_times (fmont_as_nat h5 tmp1) 15) (fmont_as_nat h5 tmp1))
{ "file_name": "code/ecdsap256/Hacl.Impl.P256.Finv.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 79, "end_line": 155, "start_col": 0, "start_line": 119 }
module Hacl.Impl.P256.Finv open FStar.Mul open FStar.HyperStack.All open FStar.HyperStack module ST = FStar.HyperStack.ST open Lib.IntTypes open Lib.Buffer open Hacl.Impl.P256.Bignum open Hacl.Impl.P256.Field module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module BD = Hacl.Spec.Bignum.Definitions module LSeq = Lib.Sequence module S = Spec.P256 module SI = Hacl.Spec.P256.Finv module SM = Hacl.Spec.P256.Montgomery #reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" unfold let linv_ctx (a:LSeq.lseq uint64 0) : Type0 = True unfold let linv (a:LSeq.lseq uint64 4) : Type0 = BD.bn_v a < S.prime unfold let refl (a:LSeq.lseq uint64 4{linv a}) : GTot S.felem = SM.from_mont (BD.bn_v a) inline_for_extraction noextract let mk_to_p256_prime_comm_monoid : BE.to_comm_monoid U64 4ul 0ul = { BE.a_spec = S.felem; BE.comm_monoid = SI.nat_mod_comm_monoid; BE.linv_ctx = linv_ctx; BE.linv = linv; BE.refl = refl; } inline_for_extraction noextract val one_mod : BE.lone_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let one_mod ctx one = make_fone one inline_for_extraction noextract val mul_mod : BE.lmul_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let mul_mod ctx x y xy = fmul xy x y inline_for_extraction noextract val sqr_mod : BE.lsqr_st U64 4ul 0ul mk_to_p256_prime_comm_monoid let sqr_mod ctx x xx = fsqr xx x inline_for_extraction noextract let mk_p256_prime_concrete_ops : BE.concrete_ops U64 4ul 0ul = { BE.to = mk_to_p256_prime_comm_monoid; BE.lone = one_mod; BE.lmul = mul_mod; BE.lsqr = sqr_mod; } inline_for_extraction noextract val fsquare_times_in_place (out:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ as_nat h out < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 out) (v b)) let fsquare_times_in_place out b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 out) (v b); BE.lexp_pow2_in_place 4ul 0ul mk_p256_prime_concrete_ops (null uint64) out b inline_for_extraction noextract val fsquare_times (out a:felem) (b:size_t) : Stack unit (requires fun h -> live h out /\ live h a /\ disjoint out a /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ as_nat h1 out < S.prime /\ fmont_as_nat h1 out == SI.fsquare_times (fmont_as_nat h0 a) (v b)) let fsquare_times out a b = let h0 = ST.get () in SE.exp_pow2_lemma SI.mk_nat_mod_concrete_ops (fmont_as_nat h0 a) (v b); BE.lexp_pow2 4ul 0ul mk_p256_prime_concrete_ops (null uint64) a b out inline_for_extraction noextract val finv_30 (x30 x2 tmp1 tmp2 a:felem) : Stack unit (requires fun h -> live h a /\ live h x30 /\ live h x2 /\ live h tmp1 /\ live h tmp2 /\ disjoint a x30 /\ disjoint a x2 /\ disjoint a tmp1 /\ disjoint a tmp2 /\ disjoint x30 x2 /\ disjoint x30 tmp1 /\ disjoint x30 tmp2 /\ disjoint x2 tmp1 /\ disjoint x2 tmp2 /\ disjoint tmp1 tmp2 /\ as_nat h a < S.prime) (ensures fun h0 _ h1 -> modifies (loc x30 |+| loc x2 |+| loc tmp1 |+| loc tmp2) h0 h1 /\ as_nat h1 x30 < S.prime /\ as_nat h1 x2 < S.prime /\ (let f = fmont_as_nat h0 a in let x2_s = S.fmul (SI.fsquare_times f 1) f in let x3_s = S.fmul (SI.fsquare_times x2_s 1) f in let x6_s = S.fmul (SI.fsquare_times x3_s 3) x3_s in let x12_s = S.fmul (SI.fsquare_times x6_s 6) x6_s in let x15_s = S.fmul (SI.fsquare_times x12_s 3) x3_s in let x30_s = S.fmul (SI.fsquare_times x15_s 15) x15_s in fmont_as_nat h1 x30 = x30_s /\ fmont_as_nat h1 x2 = x2_s))
{ "checked_file": "/", "dependencies": [ "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.P256.Montgomery.fsti.checked", "Hacl.Spec.P256.Finv.fst.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Impl.P256.Field.fsti.checked", "Hacl.Impl.P256.Bignum.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.P256.Finv.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Finv", "short_module": "SI" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Field", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Bignum", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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
x30: Hacl.Impl.P256.Bignum.felem -> x2: Hacl.Impl.P256.Bignum.felem -> tmp1: Hacl.Impl.P256.Bignum.felem -> tmp2: Hacl.Impl.P256.Bignum.felem -> a: Hacl.Impl.P256.Bignum.felem -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Impl.P256.Bignum.felem", "Prims._assert", "Prims.eq2", "Spec.P256.PointOps.felem", "Hacl.Impl.P256.Field.fmont_as_nat", "Spec.P256.PointOps.fmul", "Hacl.Spec.P256.Finv.fsquare_times", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "Hacl.Impl.P256.Field.fmul", "Hacl.Impl.P256.Finv.fsquare_times", "FStar.UInt32.__uint_to_t" ]
[]
false
true
false
false
false
let finv_30 x30 x2 tmp1 tmp2 a =
let h0 = ST.get () in fsquare_times x2 a 1ul; fmul x2 x2 a; let h1 = ST.get () in assert (fmont_as_nat h1 x2 == S.fmul (SI.fsquare_times (fmont_as_nat h0 a) 1) (fmont_as_nat h0 a)); fsquare_times x30 x2 1ul; fmul x30 x30 a; let h2 = ST.get () in assert (fmont_as_nat h2 x30 == S.fmul (SI.fsquare_times (fmont_as_nat h1 x2) 1) (fmont_as_nat h0 a)); fsquare_times tmp1 x30 3ul; fmul tmp1 tmp1 x30; let h3 = ST.get () in assert (fmont_as_nat h3 tmp1 == S.fmul (SI.fsquare_times (fmont_as_nat h2 x30) 3) (fmont_as_nat h2 x30)); fsquare_times tmp2 tmp1 6ul; fmul tmp2 tmp2 tmp1; let h4 = ST.get () in assert (fmont_as_nat h4 tmp2 == S.fmul (SI.fsquare_times (fmont_as_nat h3 tmp1) 6) (fmont_as_nat h3 tmp1)); fsquare_times tmp1 tmp2 3ul; fmul tmp1 tmp1 x30; let h5 = ST.get () in assert (fmont_as_nat h5 tmp1 == S.fmul (SI.fsquare_times (fmont_as_nat h4 tmp2) 3) (fmont_as_nat h2 x30)); fsquare_times x30 tmp1 15ul; fmul x30 x30 tmp1; let h6 = ST.get () in assert (fmont_as_nat h6 x30 == S.fmul (SI.fsquare_times (fmont_as_nat h5 tmp1) 15) (fmont_as_nat h5 tmp1))
false
LowParseWriters.fsti
LowParseWriters.returnc
val returnc (a: Type) (x: a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l )
val returnc (a: Type) (x: a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l )
let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 48, "end_line": 1005, "start_col": 0, "start_line": 1002 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> x: a -> r: LowParseWriters.LowParse.parser -> l: LowParseWriters.memory_invariant -> LowParseWriters.repr a r r (fun _ -> Prims.l_True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> Prims.l_False) l
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.memory_invariant", "LowParseWriters.Repr", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_True", "Prims.l_and", "Prims.eq2", "Prims.l_False", "LowParseWriters.return_spec", "LowParseWriters.return_impl", "LowParseWriters.repr" ]
[]
false
false
false
false
false
let returnc (a: Type) (x: a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l ) =
Repr (return_spec a x r) (return_impl a x r l)
false
LowParseWriters.fsti
LowParseWriters.rewrite_all_context_equalities
val rewrite_all_context_equalities (bs: binders) : Tac unit
val rewrite_all_context_equalities (bs: binders) : Tac unit
let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 7, "end_line": 461, "start_col": 0, "start_line": 455 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
bs: FStar.Tactics.NamedView.binders -> FStar.Tactics.Effect.Tac Prims.unit
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Tactics.NamedView.binders", "Prims.unit", "FStar.Tactics.NamedView.binder", "Prims.list", "LowParseWriters.rewrite_all_context_equalities", "FStar.Tactics.V2.Derived.try_with", "FStar.Stubs.Tactics.V2.Builtins.rewrite", "FStar.Tactics.NamedView.binder_to_binding", "Prims.exn" ]
[ "recursion" ]
false
true
false
false
false
let rec rewrite_all_context_equalities (bs: binders) : Tac unit =
match bs with | [] -> () | x_t :: bs -> (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs
false
LowParseWriters.fsti
LowParseWriters.pick_next
val pick_next (fuel: nat) : Tac bool
val pick_next (fuel: nat) : Tac bool
let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 51, "end_line": 440, "start_col": 0, "start_line": 424 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
fuel: Prims.nat -> FStar.Tactics.Effect.Tac Prims.bool
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.nat", "Prims.op_Equality", "Prims.int", "Prims.bool", "FStar.Stubs.Tactics.Types.goal", "Prims.list", "FStar.Tactics.NamedView.term", "Prims.op_AmpAmp", "LowParseWriters.pick_next", "Prims.op_Subtraction", "Prims.unit", "FStar.Tactics.V2.Derived.later", "LowParseWriters.is_uvar", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.tuple2", "LowParseWriters.is_eq", "FStar.Stubs.Tactics.Types.goal_type", "FStar.Tactics.V2.Derived.goals" ]
[ "recursion" ]
false
true
false
false
false
let rec pick_next (fuel: nat) : Tac bool =
if fuel = 0 then false else match goals () with | [] -> false | hd :: tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then (later (); pick_next (fuel - 1)) else true | None -> later (); pick_next (fuel - 1))
false
LowParseWriters.fsti
LowParseWriters.test_read1
val test_read1 (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv
val test_read1 (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv
let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 7, "end_line": 501, "start_col": 0, "start_line": 496 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
inv: LowParseWriters.memory_invariant -> f: (_: Prims.unit -> LowParseWriters.Read Prims.bool) -> LowParseWriters.Read Prims.bool
LowParseWriters.Read
[]
[]
[ "LowParseWriters.memory_invariant", "Prims.unit", "Prims.bool", "Prims.l_True" ]
[]
false
true
false
false
false
let test_read1 (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv =
let x = f () in false
false
LowParseWriters.fsti
LowParseWriters.failwith
val failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv
val failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv
let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 40, "end_line": 548, "start_col": 0, "start_line": 543 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
s: Prims.string -> LowParseWriters.ERead a
LowParseWriters.ERead
[]
[]
[ "LowParseWriters.memory_invariant", "Prims.string", "LowParseWriters.failwith_repr", "Prims.l_True", "Prims.l_False", "Prims.unit" ]
[]
false
true
false
false
false
let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv =
ERead?.reflect (failwith_repr a inv s)
false
LowParseWriters.fsti
LowParseWriters.subcomp_cond
val subcomp_cond (a: Type) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l l': memory_invariant) : GTot Type0
val subcomp_cond (a: Type) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l l': memory_invariant) : GTot Type0
let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err'
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 71, "end_line": 1095, "start_col": 0, "start_line": 1086 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> pre': LowParseWriters.pre_t r_in -> post': LowParseWriters.post_t a r_in r_out pre' -> post_err': LowParseWriters.post_err_t r_in pre' -> l: LowParseWriters.memory_invariant -> l': LowParseWriters.memory_invariant -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.l_and", "LowParseWriters.memory_invariant_includes", "LowParseWriters.subcomp_spec_cond" ]
[]
false
false
false
false
true
let subcomp_cond (a: Type) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l l': memory_invariant) : GTot Type0 =
l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err'
false
LowParseWriters.fsti
LowParseWriters.test_read2
val test_read2 (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv
val test_read2 (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv
let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 7, "end_line": 509, "start_col": 0, "start_line": 503 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
inv: LowParseWriters.memory_invariant -> f: (_: Prims.unit -> LowParseWriters.Read Prims.bool) -> LowParseWriters.Read Prims.bool
LowParseWriters.Read
[]
[]
[ "LowParseWriters.memory_invariant", "Prims.unit", "Prims.bool", "Prims.l_True" ]
[]
false
true
false
false
false
let test_read2 (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv =
let x = f () in let x = f () in false
false
LowParseWriters.fsti
LowParseWriters.deref
val deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv
val deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv
let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 32, "end_line": 724, "start_col": 0, "start_line": 718 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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: LowParseWriters.LowParse.leaf_reader p -> x: LowParseWriters.ptr p inv -> LowParseWriters.Read (Parser?.t p)
LowParseWriters.Read
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.leaf_reader", "LowParseWriters.ptr", "LowParseWriters.deref_repr", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_True", "Prims.eq2", "LowParseWriters.deref_spec" ]
[]
false
true
false
false
false
let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv =
Read?.reflect (deref_repr r x)
false
LowParseWriters.fsti
LowParseWriters.subcomp_spec_cond
val subcomp_spec_cond (a: Type) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0
val subcomp_spec_cond (a: Type) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0
let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 65, "end_line": 1083, "start_col": 0, "start_line": 1075 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> pre': LowParseWriters.pre_t r_in -> post': LowParseWriters.post_t a r_in r_out pre' -> post_err': LowParseWriters.post_err_t r_in pre' -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "Prims.l_and", "Prims.l_Forall", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_imp" ]
[]
false
false
false
false
true
let subcomp_spec_cond (a: Type) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 =
(forall v_in. pre' v_in ==> pre v_in) /\ (forall v_in x v_out. (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in. (pre' v_in /\ post_err v_in) ==> post_err' v_in)
false
LowParseWriters.fsti
LowParseWriters.failwith_spec
val failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True))
val failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True))
let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 18, "end_line": 525, "start_col": 0, "start_line": 521 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> s: Prims.string -> LowParseWriters.read_repr_spec a Prims.l_True (fun _ -> Prims.l_False) (fun _ -> Prims.l_True)
Prims.Tot
[ "total" ]
[]
[ "Prims.string", "Prims.unit", "LowParseWriters.Error", "LowParseWriters.result", "LowParseWriters.read_repr_spec", "Prims.l_True", "Prims.l_False" ]
[]
false
false
false
false
false
let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) =
fun _ -> Error s
false
LowParseWriters.fsti
LowParseWriters.buffer_sub_spec
val buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ (B.loc_buffer res) `B.loc_disjoint` inv.lwrite) (fun _ -> False))
val buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ (B.loc_buffer res) `B.loc_disjoint` inv.lwrite) (fun _ -> False))
let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 28, "end_line": 620, "start_col": 0, "start_line": 599 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
inv: LowParseWriters.memory_invariant -> b: LowStar.Buffer.buffer t -> i: FStar.UInt32.t -> len: FStar.Ghost.erased FStar.UInt32.t -> LowParseWriters.read_repr_spec (LowStar.Buffer.buffer t) (LowStar.Monotonic.Buffer.live (FStar.Ghost.reveal (Mkmemory_invariant?.h0 inv)) b /\ LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Monotonic.Buffer.loc_buffer b) (FStar.Ghost.reveal (Mkmemory_invariant?.lwrite inv)) /\ FStar.UInt32.v i + FStar.UInt32.v (FStar.Ghost.reveal len) <= LowStar.Monotonic.Buffer.length b) (fun res -> FStar.UInt32.v i + FStar.UInt32.v (FStar.Ghost.reveal len) <= LowStar.Monotonic.Buffer.length b /\ res == LowStar.Buffer.gsub b i (FStar.Ghost.reveal len) /\ LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Monotonic.Buffer.loc_buffer res) (FStar.Ghost.reveal (Mkmemory_invariant?.lwrite inv))) (fun _ -> Prims.l_False)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.memory_invariant", "LowStar.Buffer.buffer", "FStar.UInt32.t", "FStar.Ghost.erased", "Prims.unit", "LowParseWriters.Correct", "LowStar.Buffer.gsub", "FStar.Ghost.reveal", "LowParseWriters.result", "LowParseWriters.read_repr_spec", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "LowStar.Buffer.trivial_preorder", "FStar.Monotonic.HyperStack.mem", "LowParseWriters.__proj__Mkmemory_invariant__item__h0", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Monotonic.Buffer.loc", "LowParseWriters.__proj__Mkmemory_invariant__item__lwrite", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.length", "Prims.eq2", "LowStar.Monotonic.Buffer.mbuffer", "Prims.l_False" ]
[]
false
false
false
false
false
let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ (B.loc_buffer res) `B.loc_disjoint` inv.lwrite) (fun _ -> False)) =
fun _ -> Correct (B.gsub b i len)
false
LowParseWriters.fsti
LowParseWriters.test_read3
val test_read3 (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv
val test_read3 (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv
let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 7, "end_line": 519, "start_col": 0, "start_line": 512 }
(* 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) let is_uvar (t:term) : Tac bool = match inspect t with | Tv_Uvar _ _ -> true | Tv_App _ _ -> let hd, args = collect_app t in Tv_Uvar? (inspect hd) | _ -> false let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "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
inv: LowParseWriters.memory_invariant -> f: (_: Prims.unit -> LowParseWriters.Read Prims.bool) -> LowParseWriters.Read Prims.bool
LowParseWriters.Read
[]
[]
[ "LowParseWriters.memory_invariant", "Prims.unit", "Prims.bool", "Prims.l_True" ]
[]
false
true
false
false
false
let test_read3 (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv =
let x = f () in let x = f () in let x = f () in false
false