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
listlengths
0
2
mutual_with
listlengths
0
11
ideal_premises
listlengths
0
236
proof_features
listlengths
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.Sequence.Base.fst
FStar.Sequence.Base.index_into_drop_helper
val index_into_drop_helper (#ty: Type) (s: list ty) (n j: nat) : Lemma (requires j < length s - n /\ length (drop s n) = length s - n) (ensures index (drop s n) j == index s (j + n))
val index_into_drop_helper (#ty: Type) (s: list ty) (n j: nat) : Lemma (requires j < length s - n /\ length (drop s n) = length s - n) (ensures index (drop s n) j == index s (j + n))
let rec index_into_drop_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < length s - n /\ length (drop s n) = length s - n) (ensures index (drop s n) j == index s (j + n)) = match s with | hd :: tl -> if n = 0 then () else index_into_drop_helper tl (n - 1) j
{ "file_name": "ulib/experimental/FStar.Sequence.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 73, "end_line": 474, "start_col": 8, "start_line": 470 }
(* Copyright 2008-2021 Jay Lorch, Rustan Leino, Alex Summers, Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to the Dafny Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Includes material from the Dafny project (https://github.com/dafny-lang/dafny) which carries this license information: Created 9 February 2008 by Rustan Leino. Converted to Boogie 2 on 28 June 2008. Edited sequence axioms 20 October 2009 by Alex Summers. Modified 2014 by Dan Rosen. Copyright (c) 2008-2014, Microsoft. Copyright by the contributors to the Dafny Project SPDX-License-Identifier: MIT *) (** This module declares a type and functions used for modeling sequences as they're modeled in Dafny. It also states and proves some properties about sequences, and provides a lemma `all_seq_facts_lemma` one can call to bring them into context. The properties are modeled after those in the Dafny sequence axioms, with patterns for quantifiers chosen as in those axioms. @summary Type, functions, and properties of sequences *) module FStar.Sequence.Base module FLT = FStar.List.Tot /// Internally, we represent a sequence as a list. type seq (ty: Type) = list ty /// We represent the Dafny function `Seq#Length` with `length`: /// /// function Seq#Length<T>(Seq T): int; let length = FLT.length /// We represent the Dafny function `Seq#Empty` with `empty`: /// /// function Seq#Empty<T>(): Seq T; let empty (#ty: Type) : seq ty = [] /// We represent the Dafny function `Seq#Singleton` with `singleton`: /// /// function Seq#Singleton<T>(T): Seq T; let singleton (#ty: Type) (v: ty) : seq ty = [v] /// We represent the Dafny function `Seq#Index` with `index`: /// /// function Seq#Index<T>(Seq T, int): T; let index (#ty: Type) (s: seq ty) (i: nat{i < length s}) : ty = FLT.index s i /// We represent the Dafny function `Seq#Build` with `build`: /// /// function Seq#Build<T>(s: Seq T, val: T): Seq T; let build (#ty: Type) (s: seq ty) (v: ty) : seq ty = FLT.append s [v] /// We represent the Dafny function `Seq#Append` with `append`: /// /// function Seq#Append<T>(Seq T, Seq T): Seq T; let append = FLT.append /// We represent the Dafny function `Seq#Update` with `update`: /// /// function Seq#Update<T>(Seq T, int, T): Seq T; let update (#ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) : seq ty = let s1, _, s2 = FLT.split3 s i in append s1 (append [v] s2) /// We represent the Dafny function `Seq#Contains` with `contains`: /// /// function Seq#Contains<T>(Seq T, T): bool; let contains (#ty: Type) (s: seq ty) (v: ty) : Type0 = FLT.memP v s /// We represent the Dafny function `Seq#Take` with `take`: /// /// function Seq#Take<T>(s: Seq T, howMany: int): Seq T; let take (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let result, _ = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Drop` with `drop`: /// /// function Seq#Drop<T>(s: Seq T, howMany: int): Seq T; let drop (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let _, result = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Equal` with `equal`. /// /// function Seq#Equal<T>(Seq T, Seq T): bool; let equal (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 == length s1 /\ (forall j.{:pattern index s0 j \/ index s1 j} 0 <= j && j < length s0 ==> index s0 j == index s1 j) /// Instead of representing the Dafny function `Seq#SameUntil`, which /// is only ever used in Dafny to represent prefix relations, we /// instead use `is_prefix`. /// /// function Seq#SameUntil<T>(Seq T, Seq T, int): bool; let is_prefix (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 <= length s1 /\ (forall (j: nat).{:pattern index s0 j \/ index s1 j} j < length s0 ==> index s0 j == index s1 j) /// We represent the Dafny function `Seq#Rank` with `rank`. /// /// function Seq#Rank<T>(Seq T): int; let rank (#ty: Type) (v: ty) = v /// We now prove each of the facts that comprise `all_seq_facts`. /// For fact `xxx_fact`, we prove it with `xxx_lemma`. Sometimes, that /// requires a helper lemma, which we call `xxx_helper`. In some cases, /// we need multiple helpers, so we suffix their names with integers. private let length_of_empty_is_zero_lemma () : Lemma (length_of_empty_is_zero_fact) = () private let length_zero_implies_empty_lemma () : Lemma (length_zero_implies_empty_fact) = () private let singleton_length_one_lemma () : Lemma (singleton_length_one_fact) = () private let build_increments_length_lemma () : Lemma (build_increments_length_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty). length (build s v) = 1 + length s with ( FLT.append_length s [v] ) private let rec index_into_build_helper (#ty: Type) (s: list ty) (v: ty) (i: nat{i < length (append s [v])}) : Lemma (requires i <= length s) (ensures index (append s [v]) i == (if i = length s then v else index s i)) = FLT.append_length s [v]; match s with | [] -> () | hd :: tl -> if i = 0 then () else index_into_build_helper tl v (i - 1) private let index_into_build_lemma () : Lemma (requires build_increments_length_fact u#a) (ensures index_into_build_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (v: ty) (i: nat{i < length (build s v)}). (i = length s ==> index (build s v) i == v) /\ (i <> length s ==> index (build s v) i == index s i) with ( index_into_build_helper u#a s v i ) private let append_sums_lengths_lemma () : Lemma (append_sums_lengths_fact) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty). length (append s0 s1) = length s0 + length s1 with ( FLT.append_length s0 s1 ) private let index_into_singleton_lemma (_: squash (singleton_length_one_fact u#a)) : Lemma (index_into_singleton_fact u#a ()) = () private let rec index_after_append_helper (ty: Type) (s0: list ty) (s1: list ty) (n: nat) : Lemma (requires n < length (append s0 s1) && length (append s0 s1) = length s0 + length s1) (ensures index (append s0 s1) n == (if n < length s0 then index s0 n else index s1 (n - length s0))) = match s0 with | [] -> () | hd :: tl -> if n = 0 then () else index_after_append_helper ty tl s1 (n - 1) private let index_after_append_lemma (_: squash (append_sums_lengths_fact u#a)) : Lemma (index_after_append_fact u#a ()) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty) (n: nat{n < length (append s0 s1)}). (n < length s0 ==> index (append s0 s1) n == index s0 n) /\ (length s0 <= n ==> index (append s0 s1) n == index s1 (n - length s0)) with ( index_after_append_helper ty s0 s1 n ) private let rec lemma_splitAt_fst_length (#a:Type) (n:nat) (l:list a) : Lemma (requires (n <= length l)) (ensures (length (fst (FLT.splitAt n l)) = n)) = match n, l with | 0, _ -> () | _, [] -> () | _, _ :: l' -> lemma_splitAt_fst_length (n - 1) l' private let update_maintains_length_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) : Lemma (length (update s i v) = length s) = let s1, _, s2 = FLT.split3 s i in lemma_splitAt_fst_length i s; FLT.lemma_splitAt_snd_length i s; FLT.append_length [v] s2; FLT.append_length s1 (append [v] s2) private let update_maintains_length_lemma () : Lemma (update_maintains_length_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty). length (update s i v) = length s with ( update_maintains_length_helper s i v ) private let rec update_then_index_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}) : Lemma (requires n < length s) (ensures index (update s i v) n == (if i = n then v else index s n)) = match s with | hd :: tl -> if i = 0 || n = 0 then () else update_then_index_helper tl (i - 1) v (n - 1) private let update_then_index_lemma () : Lemma (update_then_index_fact) = update_maintains_length_lemma (); introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}). n < length s ==> (i = n ==> index (update s i v) n == v) /\ (i <> n ==> index (update s i v) n == index s n) with introduce _ ==> _ with given_antecedent. ( update_then_index_helper s i v n ) private let contains_iff_exists_index_lemma () : Lemma (contains_iff_exists_index_fact) = introduce forall (ty: Type) (s: seq ty) (x: ty). contains s x <==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with ( introduce contains s x ==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with given_antecedent. ( introduce exists (i: nat). i < length s /\ index s i == x with (FLT.index_of s x) and () ); introduce (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) ==> contains s x with given_antecedent. ( eliminate exists (i: nat). i < length s /\ index s i == x returns _ with _. FLT.lemma_index_memP s i ) ) private let empty_doesnt_contain_anything_lemma () : Lemma (empty_doesnt_contain_anything_fact) = () private let rec build_contains_equiv_helper (ty: Type) (s: list ty) (v: ty) (x: ty) : Lemma (FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s)) = match s with | [] -> () | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s) with _. () and _. build_contains_equiv_helper ty tl v x private let build_contains_equiv_lemma () : Lemma (build_contains_equiv_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty) (x: ty). contains (build s v) x <==> (v == x \/ contains s x) with ( build_contains_equiv_helper ty s v x ) private let rec take_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (take s n)) (ensures (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with case_x_eq_hd. assert(index s 0 == x) and case_x_ne_hd. ( take_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). i_tl < n - 1 /\ i_tl < length tl /\ index tl i_tl == x returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with _. introduce exists (i: nat). i < n /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let rec take_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (i < n /\ i < length s /\ index s i == x)) (ensures FLT.memP x (take s n)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (take s n) with case_x_eq_hd. () and case_x_ne_hd. take_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1) private let take_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (take s n) <==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = introduce FLT.memP x (take s n) ==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) with given_antecedent. (take_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) ==> FLT.memP x (take s n) with given_antecedent. ( eliminate exists (i: nat). i < n /\ i < length s /\ index s i == x returns _ with _. take_contains_equiv_exists_helper2 ty s n x i ) private let take_contains_equiv_exists_lemma () : Lemma (take_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (take s n) x <==> (exists (i: nat). i < n /\ i < length s /\ index s i == x) with ( take_contains_equiv_exists_helper3 ty s n x ) #push-options "--z3rlimit_factor 10 --fuel 1 --ifuel 1" private let rec drop_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (drop s n)) (ensures (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x with case_n_eq_0. ( eliminate x == hd \/ ~(x == hd) returns _ with _. assert(index s 0 == x) and _. ( drop_contains_equiv_exists_helper1 ty tl n x; eliminate exists (i_tl: nat). (n <= i_tl /\ i_tl < length tl /\ index tl i_tl == x) returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and () )) and case_n_ne_0. ( drop_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) #pop-options private let rec drop_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (n <= i /\ i < length s /\ index s i == x)) (ensures FLT.memP x (drop s n)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns FLT.memP x (drop s n) with _. FLT.lemma_index_memP s i and _. ( drop_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1); eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let drop_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (drop s n) <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = introduce FLT.memP x (drop s n) ==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with given_antecedent. ( drop_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) ==> FLT.memP x (drop s n) with given_antecedent. ( eliminate exists (i: nat). n <= i /\ i < length s /\ index s i == x returns _ with _. drop_contains_equiv_exists_helper2 ty s n x i ) private let drop_contains_equiv_exists_lemma () : Lemma (drop_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (drop s n) x <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with ( drop_contains_equiv_exists_helper3 ty s n x; assert (FLT.memP x (drop s n) <==> (exists (i: nat). n <= i /\ i < length s /\ index s i == x)) ) private let equal_def_lemma () : Lemma (equal_def_fact) = () private let extensionality_lemma () : Lemma (extensionality_fact) = introduce forall (ty: Type) (a: seq ty) (b: seq ty). equal a b ==> a == b with introduce _ ==> _ with given_antecedent. ( introduce forall (i: nat) . i < length a ==> index a i == index b i with introduce _ ==> _ with given_antecedent. ( assert (index a i == index b i) // needed to trigger ); FStar.List.Tot.Properties.index_extensionality a b ) private let is_prefix_def_lemma () : Lemma (is_prefix_def_fact) = () private let take_length_lemma () : Lemma (take_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (take s n) = n with introduce _ ==> _ with given_antecedent. ( lemma_splitAt_fst_length n s ) private let rec index_into_take_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < n && n <= length s /\ length (take s n) = n) (ensures index (take s n) j == index s j) = match s with | hd :: tl -> if j = 0 || n = 0 then () else index_into_take_helper tl (n - 1) (j - 1) private let index_into_take_lemma () : Lemma (requires take_length_fact u#a) (ensures index_into_take_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < n && n <= length s ==> index (take s n) j == index s j with introduce _ ==> _ with given_antecedent. ( assert (length (take s n) == n); // triggers take_length_fact index_into_take_helper s n j ) private let drop_length_lemma () : Lemma (drop_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (drop s n) = length s - n with introduce _ ==> _ with given_antecedent. ( FLT.lemma_splitAt_snd_length n s )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Properties.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked" ], "interface_file": true, "source_file": "FStar.Sequence.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "FLT" }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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.list ty -> n: Prims.nat -> j: Prims.nat -> FStar.Pervasives.Lemma (requires j < FStar.Sequence.Base.length s - n /\ FStar.Sequence.Base.length (FStar.Sequence.Base.drop s n) = FStar.Sequence.Base.length s - n ) (ensures FStar.Sequence.Base.index (FStar.Sequence.Base.drop s n) j == FStar.Sequence.Base.index s (j + n))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Prims.nat", "Prims.op_Equality", "Prims.int", "Prims.bool", "FStar.Sequence.Base.index_into_drop_helper", "Prims.op_Subtraction", "Prims.unit", "Prims.l_and", "Prims.b2t", "Prims.op_LessThan", "FStar.Sequence.Base.length", "FStar.Sequence.Base.drop", "Prims.squash", "Prims.eq2", "FStar.Sequence.Base.index", "Prims.op_Addition", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec index_into_drop_helper (#ty: Type) (s: list ty) (n j: nat) : Lemma (requires j < length s - n /\ length (drop s n) = length s - n) (ensures index (drop s n) j == index s (j + n)) =
match s with | hd :: tl -> if n = 0 then () else index_into_drop_helper tl (n - 1) j
false
FStar.Sequence.Base.fst
FStar.Sequence.Base.drop_then_drop_lemma
val drop_then_drop_lemma: Prims.unit -> Lemma (requires drop_length_fact u#a) (ensures drop_then_drop_fact u#a ())
val drop_then_drop_lemma: Prims.unit -> Lemma (requires drop_length_fact u#a) (ensures drop_then_drop_fact u#a ())
let drop_then_drop_lemma () : Lemma (requires drop_length_fact u#a) (ensures drop_then_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (m: nat) (n: nat). m + n <= length s ==> drop (drop s m) n == drop s (m + n) with introduce _ ==> _ with given_antecedent. ( assert (length (drop s m) = length s - m); // triggers drop_length_fact drop_then_drop_helper s m n )
{ "file_name": "ulib/experimental/FStar.Sequence.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 709, "start_col": 8, "start_line": 701 }
(* Copyright 2008-2021 Jay Lorch, Rustan Leino, Alex Summers, Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to the Dafny Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Includes material from the Dafny project (https://github.com/dafny-lang/dafny) which carries this license information: Created 9 February 2008 by Rustan Leino. Converted to Boogie 2 on 28 June 2008. Edited sequence axioms 20 October 2009 by Alex Summers. Modified 2014 by Dan Rosen. Copyright (c) 2008-2014, Microsoft. Copyright by the contributors to the Dafny Project SPDX-License-Identifier: MIT *) (** This module declares a type and functions used for modeling sequences as they're modeled in Dafny. It also states and proves some properties about sequences, and provides a lemma `all_seq_facts_lemma` one can call to bring them into context. The properties are modeled after those in the Dafny sequence axioms, with patterns for quantifiers chosen as in those axioms. @summary Type, functions, and properties of sequences *) module FStar.Sequence.Base module FLT = FStar.List.Tot /// Internally, we represent a sequence as a list. type seq (ty: Type) = list ty /// We represent the Dafny function `Seq#Length` with `length`: /// /// function Seq#Length<T>(Seq T): int; let length = FLT.length /// We represent the Dafny function `Seq#Empty` with `empty`: /// /// function Seq#Empty<T>(): Seq T; let empty (#ty: Type) : seq ty = [] /// We represent the Dafny function `Seq#Singleton` with `singleton`: /// /// function Seq#Singleton<T>(T): Seq T; let singleton (#ty: Type) (v: ty) : seq ty = [v] /// We represent the Dafny function `Seq#Index` with `index`: /// /// function Seq#Index<T>(Seq T, int): T; let index (#ty: Type) (s: seq ty) (i: nat{i < length s}) : ty = FLT.index s i /// We represent the Dafny function `Seq#Build` with `build`: /// /// function Seq#Build<T>(s: Seq T, val: T): Seq T; let build (#ty: Type) (s: seq ty) (v: ty) : seq ty = FLT.append s [v] /// We represent the Dafny function `Seq#Append` with `append`: /// /// function Seq#Append<T>(Seq T, Seq T): Seq T; let append = FLT.append /// We represent the Dafny function `Seq#Update` with `update`: /// /// function Seq#Update<T>(Seq T, int, T): Seq T; let update (#ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) : seq ty = let s1, _, s2 = FLT.split3 s i in append s1 (append [v] s2) /// We represent the Dafny function `Seq#Contains` with `contains`: /// /// function Seq#Contains<T>(Seq T, T): bool; let contains (#ty: Type) (s: seq ty) (v: ty) : Type0 = FLT.memP v s /// We represent the Dafny function `Seq#Take` with `take`: /// /// function Seq#Take<T>(s: Seq T, howMany: int): Seq T; let take (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let result, _ = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Drop` with `drop`: /// /// function Seq#Drop<T>(s: Seq T, howMany: int): Seq T; let drop (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let _, result = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Equal` with `equal`. /// /// function Seq#Equal<T>(Seq T, Seq T): bool; let equal (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 == length s1 /\ (forall j.{:pattern index s0 j \/ index s1 j} 0 <= j && j < length s0 ==> index s0 j == index s1 j) /// Instead of representing the Dafny function `Seq#SameUntil`, which /// is only ever used in Dafny to represent prefix relations, we /// instead use `is_prefix`. /// /// function Seq#SameUntil<T>(Seq T, Seq T, int): bool; let is_prefix (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 <= length s1 /\ (forall (j: nat).{:pattern index s0 j \/ index s1 j} j < length s0 ==> index s0 j == index s1 j) /// We represent the Dafny function `Seq#Rank` with `rank`. /// /// function Seq#Rank<T>(Seq T): int; let rank (#ty: Type) (v: ty) = v /// We now prove each of the facts that comprise `all_seq_facts`. /// For fact `xxx_fact`, we prove it with `xxx_lemma`. Sometimes, that /// requires a helper lemma, which we call `xxx_helper`. In some cases, /// we need multiple helpers, so we suffix their names with integers. private let length_of_empty_is_zero_lemma () : Lemma (length_of_empty_is_zero_fact) = () private let length_zero_implies_empty_lemma () : Lemma (length_zero_implies_empty_fact) = () private let singleton_length_one_lemma () : Lemma (singleton_length_one_fact) = () private let build_increments_length_lemma () : Lemma (build_increments_length_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty). length (build s v) = 1 + length s with ( FLT.append_length s [v] ) private let rec index_into_build_helper (#ty: Type) (s: list ty) (v: ty) (i: nat{i < length (append s [v])}) : Lemma (requires i <= length s) (ensures index (append s [v]) i == (if i = length s then v else index s i)) = FLT.append_length s [v]; match s with | [] -> () | hd :: tl -> if i = 0 then () else index_into_build_helper tl v (i - 1) private let index_into_build_lemma () : Lemma (requires build_increments_length_fact u#a) (ensures index_into_build_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (v: ty) (i: nat{i < length (build s v)}). (i = length s ==> index (build s v) i == v) /\ (i <> length s ==> index (build s v) i == index s i) with ( index_into_build_helper u#a s v i ) private let append_sums_lengths_lemma () : Lemma (append_sums_lengths_fact) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty). length (append s0 s1) = length s0 + length s1 with ( FLT.append_length s0 s1 ) private let index_into_singleton_lemma (_: squash (singleton_length_one_fact u#a)) : Lemma (index_into_singleton_fact u#a ()) = () private let rec index_after_append_helper (ty: Type) (s0: list ty) (s1: list ty) (n: nat) : Lemma (requires n < length (append s0 s1) && length (append s0 s1) = length s0 + length s1) (ensures index (append s0 s1) n == (if n < length s0 then index s0 n else index s1 (n - length s0))) = match s0 with | [] -> () | hd :: tl -> if n = 0 then () else index_after_append_helper ty tl s1 (n - 1) private let index_after_append_lemma (_: squash (append_sums_lengths_fact u#a)) : Lemma (index_after_append_fact u#a ()) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty) (n: nat{n < length (append s0 s1)}). (n < length s0 ==> index (append s0 s1) n == index s0 n) /\ (length s0 <= n ==> index (append s0 s1) n == index s1 (n - length s0)) with ( index_after_append_helper ty s0 s1 n ) private let rec lemma_splitAt_fst_length (#a:Type) (n:nat) (l:list a) : Lemma (requires (n <= length l)) (ensures (length (fst (FLT.splitAt n l)) = n)) = match n, l with | 0, _ -> () | _, [] -> () | _, _ :: l' -> lemma_splitAt_fst_length (n - 1) l' private let update_maintains_length_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) : Lemma (length (update s i v) = length s) = let s1, _, s2 = FLT.split3 s i in lemma_splitAt_fst_length i s; FLT.lemma_splitAt_snd_length i s; FLT.append_length [v] s2; FLT.append_length s1 (append [v] s2) private let update_maintains_length_lemma () : Lemma (update_maintains_length_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty). length (update s i v) = length s with ( update_maintains_length_helper s i v ) private let rec update_then_index_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}) : Lemma (requires n < length s) (ensures index (update s i v) n == (if i = n then v else index s n)) = match s with | hd :: tl -> if i = 0 || n = 0 then () else update_then_index_helper tl (i - 1) v (n - 1) private let update_then_index_lemma () : Lemma (update_then_index_fact) = update_maintains_length_lemma (); introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}). n < length s ==> (i = n ==> index (update s i v) n == v) /\ (i <> n ==> index (update s i v) n == index s n) with introduce _ ==> _ with given_antecedent. ( update_then_index_helper s i v n ) private let contains_iff_exists_index_lemma () : Lemma (contains_iff_exists_index_fact) = introduce forall (ty: Type) (s: seq ty) (x: ty). contains s x <==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with ( introduce contains s x ==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with given_antecedent. ( introduce exists (i: nat). i < length s /\ index s i == x with (FLT.index_of s x) and () ); introduce (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) ==> contains s x with given_antecedent. ( eliminate exists (i: nat). i < length s /\ index s i == x returns _ with _. FLT.lemma_index_memP s i ) ) private let empty_doesnt_contain_anything_lemma () : Lemma (empty_doesnt_contain_anything_fact) = () private let rec build_contains_equiv_helper (ty: Type) (s: list ty) (v: ty) (x: ty) : Lemma (FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s)) = match s with | [] -> () | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s) with _. () and _. build_contains_equiv_helper ty tl v x private let build_contains_equiv_lemma () : Lemma (build_contains_equiv_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty) (x: ty). contains (build s v) x <==> (v == x \/ contains s x) with ( build_contains_equiv_helper ty s v x ) private let rec take_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (take s n)) (ensures (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with case_x_eq_hd. assert(index s 0 == x) and case_x_ne_hd. ( take_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). i_tl < n - 1 /\ i_tl < length tl /\ index tl i_tl == x returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with _. introduce exists (i: nat). i < n /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let rec take_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (i < n /\ i < length s /\ index s i == x)) (ensures FLT.memP x (take s n)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (take s n) with case_x_eq_hd. () and case_x_ne_hd. take_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1) private let take_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (take s n) <==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = introduce FLT.memP x (take s n) ==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) with given_antecedent. (take_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) ==> FLT.memP x (take s n) with given_antecedent. ( eliminate exists (i: nat). i < n /\ i < length s /\ index s i == x returns _ with _. take_contains_equiv_exists_helper2 ty s n x i ) private let take_contains_equiv_exists_lemma () : Lemma (take_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (take s n) x <==> (exists (i: nat). i < n /\ i < length s /\ index s i == x) with ( take_contains_equiv_exists_helper3 ty s n x ) #push-options "--z3rlimit_factor 10 --fuel 1 --ifuel 1" private let rec drop_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (drop s n)) (ensures (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x with case_n_eq_0. ( eliminate x == hd \/ ~(x == hd) returns _ with _. assert(index s 0 == x) and _. ( drop_contains_equiv_exists_helper1 ty tl n x; eliminate exists (i_tl: nat). (n <= i_tl /\ i_tl < length tl /\ index tl i_tl == x) returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and () )) and case_n_ne_0. ( drop_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) #pop-options private let rec drop_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (n <= i /\ i < length s /\ index s i == x)) (ensures FLT.memP x (drop s n)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns FLT.memP x (drop s n) with _. FLT.lemma_index_memP s i and _. ( drop_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1); eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let drop_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (drop s n) <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = introduce FLT.memP x (drop s n) ==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with given_antecedent. ( drop_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) ==> FLT.memP x (drop s n) with given_antecedent. ( eliminate exists (i: nat). n <= i /\ i < length s /\ index s i == x returns _ with _. drop_contains_equiv_exists_helper2 ty s n x i ) private let drop_contains_equiv_exists_lemma () : Lemma (drop_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (drop s n) x <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with ( drop_contains_equiv_exists_helper3 ty s n x; assert (FLT.memP x (drop s n) <==> (exists (i: nat). n <= i /\ i < length s /\ index s i == x)) ) private let equal_def_lemma () : Lemma (equal_def_fact) = () private let extensionality_lemma () : Lemma (extensionality_fact) = introduce forall (ty: Type) (a: seq ty) (b: seq ty). equal a b ==> a == b with introduce _ ==> _ with given_antecedent. ( introduce forall (i: nat) . i < length a ==> index a i == index b i with introduce _ ==> _ with given_antecedent. ( assert (index a i == index b i) // needed to trigger ); FStar.List.Tot.Properties.index_extensionality a b ) private let is_prefix_def_lemma () : Lemma (is_prefix_def_fact) = () private let take_length_lemma () : Lemma (take_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (take s n) = n with introduce _ ==> _ with given_antecedent. ( lemma_splitAt_fst_length n s ) private let rec index_into_take_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < n && n <= length s /\ length (take s n) = n) (ensures index (take s n) j == index s j) = match s with | hd :: tl -> if j = 0 || n = 0 then () else index_into_take_helper tl (n - 1) (j - 1) private let index_into_take_lemma () : Lemma (requires take_length_fact u#a) (ensures index_into_take_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < n && n <= length s ==> index (take s n) j == index s j with introduce _ ==> _ with given_antecedent. ( assert (length (take s n) == n); // triggers take_length_fact index_into_take_helper s n j ) private let drop_length_lemma () : Lemma (drop_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (drop s n) = length s - n with introduce _ ==> _ with given_antecedent. ( FLT.lemma_splitAt_snd_length n s ) private let rec index_into_drop_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < length s - n /\ length (drop s n) = length s - n) (ensures index (drop s n) j == index s (j + n)) = match s with | hd :: tl -> if n = 0 then () else index_into_drop_helper tl (n - 1) j private let index_into_drop_lemma () : Lemma (requires drop_length_fact u#a) (ensures index_into_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < length s - n ==> index (drop s n) j == index s (j + n) with introduce _ ==> _ with given_antecedent. ( assert (length (drop s n) = length s - n); // triggers drop_length_fact index_into_drop_helper s n j ) private let drop_index_offset_lemma () : Lemma (requires drop_length_fact u#a) (ensures drop_index_offset_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (k: nat). n <= k && k < length s ==> index (drop s n) (k - n) == index s k with introduce _ ==> _ with given_antecedent. ( assert (length (drop s n) = length s - n); // triggers drop_length_fact index_into_drop_helper s n (k - n) ) private let rec append_then_take_or_drop_helper (#ty: Type) (s: list ty) (t: list ty) (n: nat) : Lemma (requires n = length s /\ length (append s t) = length s + length t) (ensures take (append s t) n == s /\ drop (append s t) n == t) = match s with | [] -> () | hd :: tl -> append_then_take_or_drop_helper tl t (n - 1) private let append_then_take_or_drop_lemma () : Lemma (requires append_sums_lengths_fact u#a) (ensures append_then_take_or_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (t: seq ty) (n: nat). n = length s ==> take (append s t) n == s /\ drop (append s t) n == t with introduce _ ==> _ with given_antecedent. ( append_then_take_or_drop_helper s t n ) #push-options "--z3rlimit 20" private let rec take_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s /\ length (take s n) = n) (ensures take (update s i v) n == update (take s n) i v) = match s with | hd :: tl -> if i = 0 then () else (update_maintains_length_lemma() ; take_commutes_with_in_range_update_helper tl (i - 1) v (n - 1)) #pop-options private let take_commutes_with_in_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a /\ take_length_fact u#a) (ensures take_commutes_with_in_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). i < n && n <= length s ==> take (update s i v) n == update (take s n) i v with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact assert (length (take s n) = n); // triggers take_length_fact take_commutes_with_in_range_update_helper s i v n ) private let rec take_ignores_out_of_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s) (ensures take (update s i v) n == take s n) = match s with | hd :: tl -> if n = 0 then () else take_ignores_out_of_range_update_helper tl (i - 1) v (n - 1) private let take_ignores_out_of_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a) (ensures take_ignores_out_of_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). n <= i && i < length s ==> take (update s i v) n == take s n with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact take_ignores_out_of_range_update_helper s i v n ) #push-options "--fuel 2 --ifuel 1 --z3rlimit_factor 4" private let rec drop_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s /\ length (drop s n) = length s - n) (ensures drop (update s i v) n == update (drop s n) (i - n) v) = match s with | hd :: tl -> if n = 0 then () else ( update_maintains_length_lemma (); drop_length_lemma (); drop_commutes_with_in_range_update_helper tl (i - 1) v (n - 1) ) #pop-options private let drop_commutes_with_in_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a /\ drop_length_fact u#a) (ensures drop_commutes_with_in_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). n <= i && i < length s ==> drop (update s i v) n == update (drop s n) (i - n) v with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact assert (length (drop s n) = length s - n); // triggers drop_length_fact drop_commutes_with_in_range_update_helper s i v n ) private let rec drop_ignores_out_of_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s) (ensures drop (update s i v) n == drop s n) = match s with | hd :: tl -> if i = 0 then () else drop_ignores_out_of_range_update_helper tl (i - 1) v (n - 1) private let drop_ignores_out_of_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a) (ensures drop_ignores_out_of_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). i < n && n <= length s ==> drop (update s i v) n == drop s n with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact drop_ignores_out_of_range_update_helper s i v n ) private let rec drop_commutes_with_build_helper (#ty: Type) (s: list ty) (v: ty) (n: nat) : Lemma (requires n <= length s /\ length (append s [v]) = 1 + length s) (ensures drop (append s [v]) n == append (drop s n) [v]) = match s with | [] -> assert (append s [v] == [v]); assert (n == 0); () | hd :: tl -> if n = 0 then () else drop_commutes_with_build_helper tl v (n - 1) private let drop_commutes_with_build_lemma () : Lemma (requires build_increments_length_fact u#a) (ensures drop_commutes_with_build_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (v: ty) (n: nat). n <= length s ==> drop (build s v) n == build (drop s n) v with introduce _ ==> _ with given_antecedent. ( assert (length (build s v) = 1 + length s); // triggers build_increments_length_fact drop_commutes_with_build_helper s v n ) private let rank_def_lemma () : Lemma (rank_def_fact) = () private let element_ranks_less_lemma () : Lemma (element_ranks_less_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat). i < length s ==> rank (index s i) << rank s with introduce _ ==> _ with given_antecedent. ( contains_iff_exists_index_lemma (); assert (contains s (index s i)); FLT.memP_precedes (index s i) s ) private let rec drop_ranks_less_helper (ty: Type) (s: list ty) (i: nat) : Lemma (requires 0 < i && i <= length s) (ensures drop s i << s) = match s with | [] -> () | hd :: tl -> if i = 1 then () else drop_ranks_less_helper ty tl (i - 1) private let drop_ranks_less_lemma () : Lemma (drop_ranks_less_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat). 0 < i && i <= length s ==> rank (drop s i) << rank s with introduce _ ==> _ with given_antecedent. ( drop_ranks_less_helper ty s i ) private let take_ranks_less_lemma () : Lemma (take_ranks_less_fact) = take_length_lemma () private let append_take_drop_ranks_less_lemma () : Lemma (append_take_drop_ranks_less_fact) = take_length_lemma (); drop_length_lemma (); append_sums_lengths_lemma () private let drop_zero_lemma () : Lemma (drop_zero_fact) = () private let take_zero_lemma () : Lemma (take_zero_fact) = () private let rec drop_then_drop_helper (#ty: Type) (s: seq ty) (m: nat) (n: nat) : Lemma (requires m + n <= length s /\ length (drop s m) = length s - m) (ensures drop (drop s m) n == drop s (m + n)) = match s with | [] -> () | hd :: tl -> if m = 0 then () else ( drop_length_lemma (); drop_then_drop_helper tl (m - 1) n )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Properties.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked" ], "interface_file": true, "source_file": "FStar.Sequence.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "FLT" }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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.Pervasives.Lemma (requires FStar.Sequence.Base.drop_length_fact) (ensures FStar.Sequence.Base.drop_then_drop_fact ())
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.unit", "FStar.Classical.Sugar.forall_intro", "Prims.l_Forall", "FStar.Sequence.Base.seq", "Prims.nat", "Prims.l_imp", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Sequence.Base.length", "Prims.eq2", "FStar.Sequence.Base.drop", "FStar.Classical.Sugar.implies_intro", "Prims.squash", "FStar.Sequence.Base.drop_then_drop_helper", "Prims._assert", "Prims.op_Equality", "Prims.int", "Prims.op_Subtraction", "FStar.Sequence.Base.drop_length_fact", "FStar.Sequence.Base.drop_then_drop_fact", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let drop_then_drop_lemma () : Lemma (requires drop_length_fact u#a) (ensures drop_then_drop_fact u#a ()) =
introduce forall (ty: Type) (s: seq ty) (m: nat) (n: nat) . m + n <= length s ==> drop (drop s m) n == drop s (m + n) with introduce _ ==> _ with given_antecedent. (assert (length (drop s m) = length s - m); drop_then_drop_helper s m n)
false
FStar.Sequence.Base.fst
FStar.Sequence.Base.drop_ignores_out_of_range_update_helper
val drop_ignores_out_of_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s) (ensures drop (update s i v) n == drop s n)
val drop_ignores_out_of_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s) (ensures drop (update s i v) n == drop s n)
let rec drop_ignores_out_of_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s) (ensures drop (update s i v) n == drop s n) = match s with | hd :: tl -> if i = 0 then () else drop_ignores_out_of_range_update_helper tl (i - 1) v (n - 1)
{ "file_name": "ulib/experimental/FStar.Sequence.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 98, "end_line": 606, "start_col": 8, "start_line": 600 }
(* Copyright 2008-2021 Jay Lorch, Rustan Leino, Alex Summers, Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to the Dafny Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Includes material from the Dafny project (https://github.com/dafny-lang/dafny) which carries this license information: Created 9 February 2008 by Rustan Leino. Converted to Boogie 2 on 28 June 2008. Edited sequence axioms 20 October 2009 by Alex Summers. Modified 2014 by Dan Rosen. Copyright (c) 2008-2014, Microsoft. Copyright by the contributors to the Dafny Project SPDX-License-Identifier: MIT *) (** This module declares a type and functions used for modeling sequences as they're modeled in Dafny. It also states and proves some properties about sequences, and provides a lemma `all_seq_facts_lemma` one can call to bring them into context. The properties are modeled after those in the Dafny sequence axioms, with patterns for quantifiers chosen as in those axioms. @summary Type, functions, and properties of sequences *) module FStar.Sequence.Base module FLT = FStar.List.Tot /// Internally, we represent a sequence as a list. type seq (ty: Type) = list ty /// We represent the Dafny function `Seq#Length` with `length`: /// /// function Seq#Length<T>(Seq T): int; let length = FLT.length /// We represent the Dafny function `Seq#Empty` with `empty`: /// /// function Seq#Empty<T>(): Seq T; let empty (#ty: Type) : seq ty = [] /// We represent the Dafny function `Seq#Singleton` with `singleton`: /// /// function Seq#Singleton<T>(T): Seq T; let singleton (#ty: Type) (v: ty) : seq ty = [v] /// We represent the Dafny function `Seq#Index` with `index`: /// /// function Seq#Index<T>(Seq T, int): T; let index (#ty: Type) (s: seq ty) (i: nat{i < length s}) : ty = FLT.index s i /// We represent the Dafny function `Seq#Build` with `build`: /// /// function Seq#Build<T>(s: Seq T, val: T): Seq T; let build (#ty: Type) (s: seq ty) (v: ty) : seq ty = FLT.append s [v] /// We represent the Dafny function `Seq#Append` with `append`: /// /// function Seq#Append<T>(Seq T, Seq T): Seq T; let append = FLT.append /// We represent the Dafny function `Seq#Update` with `update`: /// /// function Seq#Update<T>(Seq T, int, T): Seq T; let update (#ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) : seq ty = let s1, _, s2 = FLT.split3 s i in append s1 (append [v] s2) /// We represent the Dafny function `Seq#Contains` with `contains`: /// /// function Seq#Contains<T>(Seq T, T): bool; let contains (#ty: Type) (s: seq ty) (v: ty) : Type0 = FLT.memP v s /// We represent the Dafny function `Seq#Take` with `take`: /// /// function Seq#Take<T>(s: Seq T, howMany: int): Seq T; let take (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let result, _ = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Drop` with `drop`: /// /// function Seq#Drop<T>(s: Seq T, howMany: int): Seq T; let drop (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let _, result = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Equal` with `equal`. /// /// function Seq#Equal<T>(Seq T, Seq T): bool; let equal (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 == length s1 /\ (forall j.{:pattern index s0 j \/ index s1 j} 0 <= j && j < length s0 ==> index s0 j == index s1 j) /// Instead of representing the Dafny function `Seq#SameUntil`, which /// is only ever used in Dafny to represent prefix relations, we /// instead use `is_prefix`. /// /// function Seq#SameUntil<T>(Seq T, Seq T, int): bool; let is_prefix (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 <= length s1 /\ (forall (j: nat).{:pattern index s0 j \/ index s1 j} j < length s0 ==> index s0 j == index s1 j) /// We represent the Dafny function `Seq#Rank` with `rank`. /// /// function Seq#Rank<T>(Seq T): int; let rank (#ty: Type) (v: ty) = v /// We now prove each of the facts that comprise `all_seq_facts`. /// For fact `xxx_fact`, we prove it with `xxx_lemma`. Sometimes, that /// requires a helper lemma, which we call `xxx_helper`. In some cases, /// we need multiple helpers, so we suffix their names with integers. private let length_of_empty_is_zero_lemma () : Lemma (length_of_empty_is_zero_fact) = () private let length_zero_implies_empty_lemma () : Lemma (length_zero_implies_empty_fact) = () private let singleton_length_one_lemma () : Lemma (singleton_length_one_fact) = () private let build_increments_length_lemma () : Lemma (build_increments_length_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty). length (build s v) = 1 + length s with ( FLT.append_length s [v] ) private let rec index_into_build_helper (#ty: Type) (s: list ty) (v: ty) (i: nat{i < length (append s [v])}) : Lemma (requires i <= length s) (ensures index (append s [v]) i == (if i = length s then v else index s i)) = FLT.append_length s [v]; match s with | [] -> () | hd :: tl -> if i = 0 then () else index_into_build_helper tl v (i - 1) private let index_into_build_lemma () : Lemma (requires build_increments_length_fact u#a) (ensures index_into_build_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (v: ty) (i: nat{i < length (build s v)}). (i = length s ==> index (build s v) i == v) /\ (i <> length s ==> index (build s v) i == index s i) with ( index_into_build_helper u#a s v i ) private let append_sums_lengths_lemma () : Lemma (append_sums_lengths_fact) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty). length (append s0 s1) = length s0 + length s1 with ( FLT.append_length s0 s1 ) private let index_into_singleton_lemma (_: squash (singleton_length_one_fact u#a)) : Lemma (index_into_singleton_fact u#a ()) = () private let rec index_after_append_helper (ty: Type) (s0: list ty) (s1: list ty) (n: nat) : Lemma (requires n < length (append s0 s1) && length (append s0 s1) = length s0 + length s1) (ensures index (append s0 s1) n == (if n < length s0 then index s0 n else index s1 (n - length s0))) = match s0 with | [] -> () | hd :: tl -> if n = 0 then () else index_after_append_helper ty tl s1 (n - 1) private let index_after_append_lemma (_: squash (append_sums_lengths_fact u#a)) : Lemma (index_after_append_fact u#a ()) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty) (n: nat{n < length (append s0 s1)}). (n < length s0 ==> index (append s0 s1) n == index s0 n) /\ (length s0 <= n ==> index (append s0 s1) n == index s1 (n - length s0)) with ( index_after_append_helper ty s0 s1 n ) private let rec lemma_splitAt_fst_length (#a:Type) (n:nat) (l:list a) : Lemma (requires (n <= length l)) (ensures (length (fst (FLT.splitAt n l)) = n)) = match n, l with | 0, _ -> () | _, [] -> () | _, _ :: l' -> lemma_splitAt_fst_length (n - 1) l' private let update_maintains_length_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) : Lemma (length (update s i v) = length s) = let s1, _, s2 = FLT.split3 s i in lemma_splitAt_fst_length i s; FLT.lemma_splitAt_snd_length i s; FLT.append_length [v] s2; FLT.append_length s1 (append [v] s2) private let update_maintains_length_lemma () : Lemma (update_maintains_length_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty). length (update s i v) = length s with ( update_maintains_length_helper s i v ) private let rec update_then_index_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}) : Lemma (requires n < length s) (ensures index (update s i v) n == (if i = n then v else index s n)) = match s with | hd :: tl -> if i = 0 || n = 0 then () else update_then_index_helper tl (i - 1) v (n - 1) private let update_then_index_lemma () : Lemma (update_then_index_fact) = update_maintains_length_lemma (); introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}). n < length s ==> (i = n ==> index (update s i v) n == v) /\ (i <> n ==> index (update s i v) n == index s n) with introduce _ ==> _ with given_antecedent. ( update_then_index_helper s i v n ) private let contains_iff_exists_index_lemma () : Lemma (contains_iff_exists_index_fact) = introduce forall (ty: Type) (s: seq ty) (x: ty). contains s x <==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with ( introduce contains s x ==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with given_antecedent. ( introduce exists (i: nat). i < length s /\ index s i == x with (FLT.index_of s x) and () ); introduce (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) ==> contains s x with given_antecedent. ( eliminate exists (i: nat). i < length s /\ index s i == x returns _ with _. FLT.lemma_index_memP s i ) ) private let empty_doesnt_contain_anything_lemma () : Lemma (empty_doesnt_contain_anything_fact) = () private let rec build_contains_equiv_helper (ty: Type) (s: list ty) (v: ty) (x: ty) : Lemma (FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s)) = match s with | [] -> () | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s) with _. () and _. build_contains_equiv_helper ty tl v x private let build_contains_equiv_lemma () : Lemma (build_contains_equiv_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty) (x: ty). contains (build s v) x <==> (v == x \/ contains s x) with ( build_contains_equiv_helper ty s v x ) private let rec take_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (take s n)) (ensures (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with case_x_eq_hd. assert(index s 0 == x) and case_x_ne_hd. ( take_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). i_tl < n - 1 /\ i_tl < length tl /\ index tl i_tl == x returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with _. introduce exists (i: nat). i < n /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let rec take_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (i < n /\ i < length s /\ index s i == x)) (ensures FLT.memP x (take s n)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (take s n) with case_x_eq_hd. () and case_x_ne_hd. take_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1) private let take_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (take s n) <==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = introduce FLT.memP x (take s n) ==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) with given_antecedent. (take_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) ==> FLT.memP x (take s n) with given_antecedent. ( eliminate exists (i: nat). i < n /\ i < length s /\ index s i == x returns _ with _. take_contains_equiv_exists_helper2 ty s n x i ) private let take_contains_equiv_exists_lemma () : Lemma (take_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (take s n) x <==> (exists (i: nat). i < n /\ i < length s /\ index s i == x) with ( take_contains_equiv_exists_helper3 ty s n x ) #push-options "--z3rlimit_factor 10 --fuel 1 --ifuel 1" private let rec drop_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (drop s n)) (ensures (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x with case_n_eq_0. ( eliminate x == hd \/ ~(x == hd) returns _ with _. assert(index s 0 == x) and _. ( drop_contains_equiv_exists_helper1 ty tl n x; eliminate exists (i_tl: nat). (n <= i_tl /\ i_tl < length tl /\ index tl i_tl == x) returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and () )) and case_n_ne_0. ( drop_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) #pop-options private let rec drop_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (n <= i /\ i < length s /\ index s i == x)) (ensures FLT.memP x (drop s n)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns FLT.memP x (drop s n) with _. FLT.lemma_index_memP s i and _. ( drop_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1); eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let drop_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (drop s n) <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = introduce FLT.memP x (drop s n) ==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with given_antecedent. ( drop_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) ==> FLT.memP x (drop s n) with given_antecedent. ( eliminate exists (i: nat). n <= i /\ i < length s /\ index s i == x returns _ with _. drop_contains_equiv_exists_helper2 ty s n x i ) private let drop_contains_equiv_exists_lemma () : Lemma (drop_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (drop s n) x <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with ( drop_contains_equiv_exists_helper3 ty s n x; assert (FLT.memP x (drop s n) <==> (exists (i: nat). n <= i /\ i < length s /\ index s i == x)) ) private let equal_def_lemma () : Lemma (equal_def_fact) = () private let extensionality_lemma () : Lemma (extensionality_fact) = introduce forall (ty: Type) (a: seq ty) (b: seq ty). equal a b ==> a == b with introduce _ ==> _ with given_antecedent. ( introduce forall (i: nat) . i < length a ==> index a i == index b i with introduce _ ==> _ with given_antecedent. ( assert (index a i == index b i) // needed to trigger ); FStar.List.Tot.Properties.index_extensionality a b ) private let is_prefix_def_lemma () : Lemma (is_prefix_def_fact) = () private let take_length_lemma () : Lemma (take_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (take s n) = n with introduce _ ==> _ with given_antecedent. ( lemma_splitAt_fst_length n s ) private let rec index_into_take_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < n && n <= length s /\ length (take s n) = n) (ensures index (take s n) j == index s j) = match s with | hd :: tl -> if j = 0 || n = 0 then () else index_into_take_helper tl (n - 1) (j - 1) private let index_into_take_lemma () : Lemma (requires take_length_fact u#a) (ensures index_into_take_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < n && n <= length s ==> index (take s n) j == index s j with introduce _ ==> _ with given_antecedent. ( assert (length (take s n) == n); // triggers take_length_fact index_into_take_helper s n j ) private let drop_length_lemma () : Lemma (drop_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (drop s n) = length s - n with introduce _ ==> _ with given_antecedent. ( FLT.lemma_splitAt_snd_length n s ) private let rec index_into_drop_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < length s - n /\ length (drop s n) = length s - n) (ensures index (drop s n) j == index s (j + n)) = match s with | hd :: tl -> if n = 0 then () else index_into_drop_helper tl (n - 1) j private let index_into_drop_lemma () : Lemma (requires drop_length_fact u#a) (ensures index_into_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < length s - n ==> index (drop s n) j == index s (j + n) with introduce _ ==> _ with given_antecedent. ( assert (length (drop s n) = length s - n); // triggers drop_length_fact index_into_drop_helper s n j ) private let drop_index_offset_lemma () : Lemma (requires drop_length_fact u#a) (ensures drop_index_offset_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (k: nat). n <= k && k < length s ==> index (drop s n) (k - n) == index s k with introduce _ ==> _ with given_antecedent. ( assert (length (drop s n) = length s - n); // triggers drop_length_fact index_into_drop_helper s n (k - n) ) private let rec append_then_take_or_drop_helper (#ty: Type) (s: list ty) (t: list ty) (n: nat) : Lemma (requires n = length s /\ length (append s t) = length s + length t) (ensures take (append s t) n == s /\ drop (append s t) n == t) = match s with | [] -> () | hd :: tl -> append_then_take_or_drop_helper tl t (n - 1) private let append_then_take_or_drop_lemma () : Lemma (requires append_sums_lengths_fact u#a) (ensures append_then_take_or_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (t: seq ty) (n: nat). n = length s ==> take (append s t) n == s /\ drop (append s t) n == t with introduce _ ==> _ with given_antecedent. ( append_then_take_or_drop_helper s t n ) #push-options "--z3rlimit 20" private let rec take_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s /\ length (take s n) = n) (ensures take (update s i v) n == update (take s n) i v) = match s with | hd :: tl -> if i = 0 then () else (update_maintains_length_lemma() ; take_commutes_with_in_range_update_helper tl (i - 1) v (n - 1)) #pop-options private let take_commutes_with_in_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a /\ take_length_fact u#a) (ensures take_commutes_with_in_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). i < n && n <= length s ==> take (update s i v) n == update (take s n) i v with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact assert (length (take s n) = n); // triggers take_length_fact take_commutes_with_in_range_update_helper s i v n ) private let rec take_ignores_out_of_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s) (ensures take (update s i v) n == take s n) = match s with | hd :: tl -> if n = 0 then () else take_ignores_out_of_range_update_helper tl (i - 1) v (n - 1) private let take_ignores_out_of_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a) (ensures take_ignores_out_of_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). n <= i && i < length s ==> take (update s i v) n == take s n with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact take_ignores_out_of_range_update_helper s i v n ) #push-options "--fuel 2 --ifuel 1 --z3rlimit_factor 4" private let rec drop_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s /\ length (drop s n) = length s - n) (ensures drop (update s i v) n == update (drop s n) (i - n) v) = match s with | hd :: tl -> if n = 0 then () else ( update_maintains_length_lemma (); drop_length_lemma (); drop_commutes_with_in_range_update_helper tl (i - 1) v (n - 1) ) #pop-options private let drop_commutes_with_in_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a /\ drop_length_fact u#a) (ensures drop_commutes_with_in_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). n <= i && i < length s ==> drop (update s i v) n == update (drop s n) (i - n) v with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact assert (length (drop s n) = length s - n); // triggers drop_length_fact drop_commutes_with_in_range_update_helper s i v n )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Properties.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked" ], "interface_file": true, "source_file": "FStar.Sequence.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "FLT" }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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.list ty -> i: Prims.nat -> v: ty -> n: Prims.nat -> FStar.Pervasives.Lemma (requires i < n /\ n <= FStar.Sequence.Base.length s /\ FStar.Sequence.Base.length (FStar.Sequence.Base.update s i v) = FStar.Sequence.Base.length s ) (ensures FStar.Sequence.Base.drop (FStar.Sequence.Base.update s i v) n == FStar.Sequence.Base.drop s n)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Prims.nat", "Prims.op_Equality", "Prims.int", "Prims.bool", "FStar.Sequence.Base.drop_ignores_out_of_range_update_helper", "Prims.op_Subtraction", "Prims.unit", "Prims.l_and", "Prims.b2t", "Prims.op_LessThan", "Prims.op_LessThanOrEqual", "FStar.Sequence.Base.length", "FStar.Sequence.Base.update", "Prims.squash", "Prims.eq2", "FStar.Sequence.Base.seq", "FStar.Sequence.Base.drop", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec drop_ignores_out_of_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s) (ensures drop (update s i v) n == drop s n) =
match s with | hd :: tl -> if i = 0 then () else drop_ignores_out_of_range_update_helper tl (i - 1) v (n - 1)
false
Pulse.Checker.Prover.ElimExists.fst
Pulse.Checker.Prover.ElimExists.should_elim_exists
val should_elim_exists (v: vprop) : T.Tac bool
val should_elim_exists (v: vprop) : T.Tac bool
let should_elim_exists (v:vprop) : T.Tac bool = match v.t with | Tm_ExistsSL _ _ _ -> true | _ -> false
{ "file_name": "lib/steel/pulse/Pulse.Checker.Prover.ElimExists.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 14, "end_line": 33, "start_col": 0, "start_line": 30 }
(* 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.Checker.Prover.ElimExists open Pulse.Syntax open Pulse.Typing open Pulse.Typing.Combinators module T = FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open Pulse.Checker.VPropEquiv open Pulse.Checker.Prover.Base
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.Combinators.fsti.checked", "Pulse.Typing.fst.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.Checker.VPropEquiv.fsti.checked", "Pulse.Checker.Prover.Base.fsti.checked", "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Pulse.Checker.Prover.ElimExists.fst" }
[ { "abbrev": false, "full_module": "Pulse.Checker.Prover.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.VPropEquiv", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "Pulse.Typing.Combinators", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "Pulse.Checker.Prover.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Prover", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Prover", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: Pulse.Syntax.Base.vprop -> FStar.Tactics.Effect.Tac Prims.bool
FStar.Tactics.Effect.Tac
[]
[]
[ "Pulse.Syntax.Base.vprop", "Pulse.Syntax.Base.__proj__Mkterm__item__t", "Pulse.Syntax.Base.universe", "Pulse.Syntax.Base.binder", "Pulse.Syntax.Base.term", "Pulse.Syntax.Base.term'", "Prims.bool" ]
[]
false
true
false
false
false
let should_elim_exists (v: vprop) : T.Tac bool =
match v.t with | Tm_ExistsSL _ _ _ -> true | _ -> false
false
FStar.Sequence.Base.fst
FStar.Sequence.Base.take_commutes_with_in_range_update_helper
val take_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s /\ length (take s n) = n) (ensures take (update s i v) n == update (take s n) i v)
val take_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s /\ length (take s n) = n) (ensures take (update s i v) n == update (take s n) i v)
let rec take_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s /\ length (take s n) = n) (ensures take (update s i v) n == update (take s n) i v) = match s with | hd :: tl -> if i = 0 then () else (update_maintains_length_lemma() ; take_commutes_with_in_range_update_helper tl (i - 1) v (n - 1))
{ "file_name": "ulib/experimental/FStar.Sequence.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 136, "end_line": 526, "start_col": 8, "start_line": 519 }
(* Copyright 2008-2021 Jay Lorch, Rustan Leino, Alex Summers, Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to the Dafny Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Includes material from the Dafny project (https://github.com/dafny-lang/dafny) which carries this license information: Created 9 February 2008 by Rustan Leino. Converted to Boogie 2 on 28 June 2008. Edited sequence axioms 20 October 2009 by Alex Summers. Modified 2014 by Dan Rosen. Copyright (c) 2008-2014, Microsoft. Copyright by the contributors to the Dafny Project SPDX-License-Identifier: MIT *) (** This module declares a type and functions used for modeling sequences as they're modeled in Dafny. It also states and proves some properties about sequences, and provides a lemma `all_seq_facts_lemma` one can call to bring them into context. The properties are modeled after those in the Dafny sequence axioms, with patterns for quantifiers chosen as in those axioms. @summary Type, functions, and properties of sequences *) module FStar.Sequence.Base module FLT = FStar.List.Tot /// Internally, we represent a sequence as a list. type seq (ty: Type) = list ty /// We represent the Dafny function `Seq#Length` with `length`: /// /// function Seq#Length<T>(Seq T): int; let length = FLT.length /// We represent the Dafny function `Seq#Empty` with `empty`: /// /// function Seq#Empty<T>(): Seq T; let empty (#ty: Type) : seq ty = [] /// We represent the Dafny function `Seq#Singleton` with `singleton`: /// /// function Seq#Singleton<T>(T): Seq T; let singleton (#ty: Type) (v: ty) : seq ty = [v] /// We represent the Dafny function `Seq#Index` with `index`: /// /// function Seq#Index<T>(Seq T, int): T; let index (#ty: Type) (s: seq ty) (i: nat{i < length s}) : ty = FLT.index s i /// We represent the Dafny function `Seq#Build` with `build`: /// /// function Seq#Build<T>(s: Seq T, val: T): Seq T; let build (#ty: Type) (s: seq ty) (v: ty) : seq ty = FLT.append s [v] /// We represent the Dafny function `Seq#Append` with `append`: /// /// function Seq#Append<T>(Seq T, Seq T): Seq T; let append = FLT.append /// We represent the Dafny function `Seq#Update` with `update`: /// /// function Seq#Update<T>(Seq T, int, T): Seq T; let update (#ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) : seq ty = let s1, _, s2 = FLT.split3 s i in append s1 (append [v] s2) /// We represent the Dafny function `Seq#Contains` with `contains`: /// /// function Seq#Contains<T>(Seq T, T): bool; let contains (#ty: Type) (s: seq ty) (v: ty) : Type0 = FLT.memP v s /// We represent the Dafny function `Seq#Take` with `take`: /// /// function Seq#Take<T>(s: Seq T, howMany: int): Seq T; let take (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let result, _ = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Drop` with `drop`: /// /// function Seq#Drop<T>(s: Seq T, howMany: int): Seq T; let drop (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let _, result = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Equal` with `equal`. /// /// function Seq#Equal<T>(Seq T, Seq T): bool; let equal (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 == length s1 /\ (forall j.{:pattern index s0 j \/ index s1 j} 0 <= j && j < length s0 ==> index s0 j == index s1 j) /// Instead of representing the Dafny function `Seq#SameUntil`, which /// is only ever used in Dafny to represent prefix relations, we /// instead use `is_prefix`. /// /// function Seq#SameUntil<T>(Seq T, Seq T, int): bool; let is_prefix (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 <= length s1 /\ (forall (j: nat).{:pattern index s0 j \/ index s1 j} j < length s0 ==> index s0 j == index s1 j) /// We represent the Dafny function `Seq#Rank` with `rank`. /// /// function Seq#Rank<T>(Seq T): int; let rank (#ty: Type) (v: ty) = v /// We now prove each of the facts that comprise `all_seq_facts`. /// For fact `xxx_fact`, we prove it with `xxx_lemma`. Sometimes, that /// requires a helper lemma, which we call `xxx_helper`. In some cases, /// we need multiple helpers, so we suffix their names with integers. private let length_of_empty_is_zero_lemma () : Lemma (length_of_empty_is_zero_fact) = () private let length_zero_implies_empty_lemma () : Lemma (length_zero_implies_empty_fact) = () private let singleton_length_one_lemma () : Lemma (singleton_length_one_fact) = () private let build_increments_length_lemma () : Lemma (build_increments_length_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty). length (build s v) = 1 + length s with ( FLT.append_length s [v] ) private let rec index_into_build_helper (#ty: Type) (s: list ty) (v: ty) (i: nat{i < length (append s [v])}) : Lemma (requires i <= length s) (ensures index (append s [v]) i == (if i = length s then v else index s i)) = FLT.append_length s [v]; match s with | [] -> () | hd :: tl -> if i = 0 then () else index_into_build_helper tl v (i - 1) private let index_into_build_lemma () : Lemma (requires build_increments_length_fact u#a) (ensures index_into_build_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (v: ty) (i: nat{i < length (build s v)}). (i = length s ==> index (build s v) i == v) /\ (i <> length s ==> index (build s v) i == index s i) with ( index_into_build_helper u#a s v i ) private let append_sums_lengths_lemma () : Lemma (append_sums_lengths_fact) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty). length (append s0 s1) = length s0 + length s1 with ( FLT.append_length s0 s1 ) private let index_into_singleton_lemma (_: squash (singleton_length_one_fact u#a)) : Lemma (index_into_singleton_fact u#a ()) = () private let rec index_after_append_helper (ty: Type) (s0: list ty) (s1: list ty) (n: nat) : Lemma (requires n < length (append s0 s1) && length (append s0 s1) = length s0 + length s1) (ensures index (append s0 s1) n == (if n < length s0 then index s0 n else index s1 (n - length s0))) = match s0 with | [] -> () | hd :: tl -> if n = 0 then () else index_after_append_helper ty tl s1 (n - 1) private let index_after_append_lemma (_: squash (append_sums_lengths_fact u#a)) : Lemma (index_after_append_fact u#a ()) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty) (n: nat{n < length (append s0 s1)}). (n < length s0 ==> index (append s0 s1) n == index s0 n) /\ (length s0 <= n ==> index (append s0 s1) n == index s1 (n - length s0)) with ( index_after_append_helper ty s0 s1 n ) private let rec lemma_splitAt_fst_length (#a:Type) (n:nat) (l:list a) : Lemma (requires (n <= length l)) (ensures (length (fst (FLT.splitAt n l)) = n)) = match n, l with | 0, _ -> () | _, [] -> () | _, _ :: l' -> lemma_splitAt_fst_length (n - 1) l' private let update_maintains_length_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) : Lemma (length (update s i v) = length s) = let s1, _, s2 = FLT.split3 s i in lemma_splitAt_fst_length i s; FLT.lemma_splitAt_snd_length i s; FLT.append_length [v] s2; FLT.append_length s1 (append [v] s2) private let update_maintains_length_lemma () : Lemma (update_maintains_length_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty). length (update s i v) = length s with ( update_maintains_length_helper s i v ) private let rec update_then_index_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}) : Lemma (requires n < length s) (ensures index (update s i v) n == (if i = n then v else index s n)) = match s with | hd :: tl -> if i = 0 || n = 0 then () else update_then_index_helper tl (i - 1) v (n - 1) private let update_then_index_lemma () : Lemma (update_then_index_fact) = update_maintains_length_lemma (); introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}). n < length s ==> (i = n ==> index (update s i v) n == v) /\ (i <> n ==> index (update s i v) n == index s n) with introduce _ ==> _ with given_antecedent. ( update_then_index_helper s i v n ) private let contains_iff_exists_index_lemma () : Lemma (contains_iff_exists_index_fact) = introduce forall (ty: Type) (s: seq ty) (x: ty). contains s x <==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with ( introduce contains s x ==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with given_antecedent. ( introduce exists (i: nat). i < length s /\ index s i == x with (FLT.index_of s x) and () ); introduce (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) ==> contains s x with given_antecedent. ( eliminate exists (i: nat). i < length s /\ index s i == x returns _ with _. FLT.lemma_index_memP s i ) ) private let empty_doesnt_contain_anything_lemma () : Lemma (empty_doesnt_contain_anything_fact) = () private let rec build_contains_equiv_helper (ty: Type) (s: list ty) (v: ty) (x: ty) : Lemma (FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s)) = match s with | [] -> () | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s) with _. () and _. build_contains_equiv_helper ty tl v x private let build_contains_equiv_lemma () : Lemma (build_contains_equiv_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty) (x: ty). contains (build s v) x <==> (v == x \/ contains s x) with ( build_contains_equiv_helper ty s v x ) private let rec take_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (take s n)) (ensures (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with case_x_eq_hd. assert(index s 0 == x) and case_x_ne_hd. ( take_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). i_tl < n - 1 /\ i_tl < length tl /\ index tl i_tl == x returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with _. introduce exists (i: nat). i < n /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let rec take_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (i < n /\ i < length s /\ index s i == x)) (ensures FLT.memP x (take s n)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (take s n) with case_x_eq_hd. () and case_x_ne_hd. take_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1) private let take_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (take s n) <==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = introduce FLT.memP x (take s n) ==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) with given_antecedent. (take_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) ==> FLT.memP x (take s n) with given_antecedent. ( eliminate exists (i: nat). i < n /\ i < length s /\ index s i == x returns _ with _. take_contains_equiv_exists_helper2 ty s n x i ) private let take_contains_equiv_exists_lemma () : Lemma (take_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (take s n) x <==> (exists (i: nat). i < n /\ i < length s /\ index s i == x) with ( take_contains_equiv_exists_helper3 ty s n x ) #push-options "--z3rlimit_factor 10 --fuel 1 --ifuel 1" private let rec drop_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (drop s n)) (ensures (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x with case_n_eq_0. ( eliminate x == hd \/ ~(x == hd) returns _ with _. assert(index s 0 == x) and _. ( drop_contains_equiv_exists_helper1 ty tl n x; eliminate exists (i_tl: nat). (n <= i_tl /\ i_tl < length tl /\ index tl i_tl == x) returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and () )) and case_n_ne_0. ( drop_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) #pop-options private let rec drop_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (n <= i /\ i < length s /\ index s i == x)) (ensures FLT.memP x (drop s n)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns FLT.memP x (drop s n) with _. FLT.lemma_index_memP s i and _. ( drop_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1); eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let drop_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (drop s n) <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = introduce FLT.memP x (drop s n) ==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with given_antecedent. ( drop_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) ==> FLT.memP x (drop s n) with given_antecedent. ( eliminate exists (i: nat). n <= i /\ i < length s /\ index s i == x returns _ with _. drop_contains_equiv_exists_helper2 ty s n x i ) private let drop_contains_equiv_exists_lemma () : Lemma (drop_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (drop s n) x <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with ( drop_contains_equiv_exists_helper3 ty s n x; assert (FLT.memP x (drop s n) <==> (exists (i: nat). n <= i /\ i < length s /\ index s i == x)) ) private let equal_def_lemma () : Lemma (equal_def_fact) = () private let extensionality_lemma () : Lemma (extensionality_fact) = introduce forall (ty: Type) (a: seq ty) (b: seq ty). equal a b ==> a == b with introduce _ ==> _ with given_antecedent. ( introduce forall (i: nat) . i < length a ==> index a i == index b i with introduce _ ==> _ with given_antecedent. ( assert (index a i == index b i) // needed to trigger ); FStar.List.Tot.Properties.index_extensionality a b ) private let is_prefix_def_lemma () : Lemma (is_prefix_def_fact) = () private let take_length_lemma () : Lemma (take_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (take s n) = n with introduce _ ==> _ with given_antecedent. ( lemma_splitAt_fst_length n s ) private let rec index_into_take_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < n && n <= length s /\ length (take s n) = n) (ensures index (take s n) j == index s j) = match s with | hd :: tl -> if j = 0 || n = 0 then () else index_into_take_helper tl (n - 1) (j - 1) private let index_into_take_lemma () : Lemma (requires take_length_fact u#a) (ensures index_into_take_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < n && n <= length s ==> index (take s n) j == index s j with introduce _ ==> _ with given_antecedent. ( assert (length (take s n) == n); // triggers take_length_fact index_into_take_helper s n j ) private let drop_length_lemma () : Lemma (drop_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (drop s n) = length s - n with introduce _ ==> _ with given_antecedent. ( FLT.lemma_splitAt_snd_length n s ) private let rec index_into_drop_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < length s - n /\ length (drop s n) = length s - n) (ensures index (drop s n) j == index s (j + n)) = match s with | hd :: tl -> if n = 0 then () else index_into_drop_helper tl (n - 1) j private let index_into_drop_lemma () : Lemma (requires drop_length_fact u#a) (ensures index_into_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < length s - n ==> index (drop s n) j == index s (j + n) with introduce _ ==> _ with given_antecedent. ( assert (length (drop s n) = length s - n); // triggers drop_length_fact index_into_drop_helper s n j ) private let drop_index_offset_lemma () : Lemma (requires drop_length_fact u#a) (ensures drop_index_offset_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (k: nat). n <= k && k < length s ==> index (drop s n) (k - n) == index s k with introduce _ ==> _ with given_antecedent. ( assert (length (drop s n) = length s - n); // triggers drop_length_fact index_into_drop_helper s n (k - n) ) private let rec append_then_take_or_drop_helper (#ty: Type) (s: list ty) (t: list ty) (n: nat) : Lemma (requires n = length s /\ length (append s t) = length s + length t) (ensures take (append s t) n == s /\ drop (append s t) n == t) = match s with | [] -> () | hd :: tl -> append_then_take_or_drop_helper tl t (n - 1) private let append_then_take_or_drop_lemma () : Lemma (requires append_sums_lengths_fact u#a) (ensures append_then_take_or_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (t: seq ty) (n: nat). n = length s ==> take (append s t) n == s /\ drop (append s t) n == t with introduce _ ==> _ with given_antecedent. ( append_then_take_or_drop_helper s t n )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Properties.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked" ], "interface_file": true, "source_file": "FStar.Sequence.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "FLT" }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_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
s: Prims.list ty -> i: Prims.nat -> v: ty -> n: Prims.nat -> FStar.Pervasives.Lemma (requires i < n /\ n <= FStar.Sequence.Base.length s /\ FStar.Sequence.Base.length (FStar.Sequence.Base.update s i v) = FStar.Sequence.Base.length s /\ FStar.Sequence.Base.length (FStar.Sequence.Base.take s n) = n) (ensures FStar.Sequence.Base.take (FStar.Sequence.Base.update s i v) n == FStar.Sequence.Base.update (FStar.Sequence.Base.take s n) i v)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Prims.nat", "Prims.op_Equality", "Prims.int", "Prims.bool", "FStar.Sequence.Base.take_commutes_with_in_range_update_helper", "Prims.op_Subtraction", "Prims.unit", "FStar.Sequence.Base.update_maintains_length_lemma", "Prims.l_and", "Prims.b2t", "Prims.op_LessThan", "Prims.op_LessThanOrEqual", "FStar.Sequence.Base.length", "FStar.Sequence.Base.update", "FStar.Sequence.Base.take", "Prims.squash", "Prims.eq2", "FStar.Sequence.Base.seq", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec take_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s /\ length (take s n) = n) (ensures take (update s i v) n == update (take s n) i v) =
match s with | hd :: tl -> if i = 0 then () else (update_maintains_length_lemma (); take_commutes_with_in_range_update_helper tl (i - 1) v (n - 1))
false
AlgWP.fst
AlgWP.rwops
val rwops : Type0
let rwops = labs:ops{sublist labs [Read; Write]}
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 48, "end_line": 17, "start_col": 0, "start_line": 17 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type0
Prims.Tot
[ "total" ]
[]
[ "Alg.ops", "Alg.sublist", "Prims.Cons", "Alg.op", "Alg.Read", "Alg.Write", "Prims.Nil" ]
[]
false
false
false
true
true
let rwops =
labs: ops{sublist labs [Read; Write]}
false
AlgWP.fst
AlgWP.subops
val subops: rwops -> rwops -> Type0
val subops: rwops -> rwops -> Type0
let subops : rwops -> rwops -> Type0 = sublist
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 46, "end_line": 33, "start_col": 0, "start_line": 33 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
_: AlgWP.rwops -> _: AlgWP.rwops -> Type0
Prims.Tot
[ "total" ]
[]
[ "Alg.sublist" ]
[]
false
false
false
true
true
let subops: rwops -> rwops -> Type0 =
sublist
false
AlgWP.fst
AlgWP.sublist_at
val sublist_at (l1 l2: ops) : Lemma (sublist l1 (l1 @ l2) /\ sublist l2 (l1 @ l2)) [SMTPat (l1 @ l2)]
val sublist_at (l1 l2: ops) : Lemma (sublist l1 (l1 @ l2) /\ sublist l2 (l1 @ l2)) [SMTPat (l1 @ l2)]
let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 24, "end_line": 38, "start_col": 0, "start_line": 35 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
l1: Alg.ops -> l2: Alg.ops -> FStar.Pervasives.Lemma (ensures Alg.sublist l1 (l1 @ l2) /\ Alg.sublist l2 (l1 @ l2)) [SMTPat (l1 @ l2)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Alg.ops", "Alg.sublist_at", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.l_and", "Alg.sublist", "FStar.List.Tot.Base.op_At", "Alg.op", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.list", "Prims.Nil" ]
[]
true
false
true
false
false
let sublist_at (l1 l2: ops) : Lemma (sublist l1 (l1 @ l2) /\ sublist l2 (l1 @ l2)) [SMTPat (l1 @ l2)] =
Alg.sublist_at l1 l2
false
FStar.Sequence.Base.fst
FStar.Sequence.Base.drop_then_drop_helper
val drop_then_drop_helper (#ty: Type) (s: seq ty) (m n: nat) : Lemma (requires m + n <= length s /\ length (drop s m) = length s - m) (ensures drop (drop s m) n == drop s (m + n))
val drop_then_drop_helper (#ty: Type) (s: seq ty) (m n: nat) : Lemma (requires m + n <= length s /\ length (drop s m) = length s - m) (ensures drop (drop s m) n == drop s (m + n))
let rec drop_then_drop_helper (#ty: Type) (s: seq ty) (m: nat) (n: nat) : Lemma (requires m + n <= length s /\ length (drop s m) = length s - m) (ensures drop (drop s m) n == drop s (m + n)) = match s with | [] -> () | hd :: tl -> if m = 0 then () else ( drop_length_lemma (); drop_then_drop_helper tl (m - 1) n )
{ "file_name": "ulib/experimental/FStar.Sequence.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 699, "start_col": 8, "start_line": 688 }
(* Copyright 2008-2021 Jay Lorch, Rustan Leino, Alex Summers, Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to the Dafny Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Includes material from the Dafny project (https://github.com/dafny-lang/dafny) which carries this license information: Created 9 February 2008 by Rustan Leino. Converted to Boogie 2 on 28 June 2008. Edited sequence axioms 20 October 2009 by Alex Summers. Modified 2014 by Dan Rosen. Copyright (c) 2008-2014, Microsoft. Copyright by the contributors to the Dafny Project SPDX-License-Identifier: MIT *) (** This module declares a type and functions used for modeling sequences as they're modeled in Dafny. It also states and proves some properties about sequences, and provides a lemma `all_seq_facts_lemma` one can call to bring them into context. The properties are modeled after those in the Dafny sequence axioms, with patterns for quantifiers chosen as in those axioms. @summary Type, functions, and properties of sequences *) module FStar.Sequence.Base module FLT = FStar.List.Tot /// Internally, we represent a sequence as a list. type seq (ty: Type) = list ty /// We represent the Dafny function `Seq#Length` with `length`: /// /// function Seq#Length<T>(Seq T): int; let length = FLT.length /// We represent the Dafny function `Seq#Empty` with `empty`: /// /// function Seq#Empty<T>(): Seq T; let empty (#ty: Type) : seq ty = [] /// We represent the Dafny function `Seq#Singleton` with `singleton`: /// /// function Seq#Singleton<T>(T): Seq T; let singleton (#ty: Type) (v: ty) : seq ty = [v] /// We represent the Dafny function `Seq#Index` with `index`: /// /// function Seq#Index<T>(Seq T, int): T; let index (#ty: Type) (s: seq ty) (i: nat{i < length s}) : ty = FLT.index s i /// We represent the Dafny function `Seq#Build` with `build`: /// /// function Seq#Build<T>(s: Seq T, val: T): Seq T; let build (#ty: Type) (s: seq ty) (v: ty) : seq ty = FLT.append s [v] /// We represent the Dafny function `Seq#Append` with `append`: /// /// function Seq#Append<T>(Seq T, Seq T): Seq T; let append = FLT.append /// We represent the Dafny function `Seq#Update` with `update`: /// /// function Seq#Update<T>(Seq T, int, T): Seq T; let update (#ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) : seq ty = let s1, _, s2 = FLT.split3 s i in append s1 (append [v] s2) /// We represent the Dafny function `Seq#Contains` with `contains`: /// /// function Seq#Contains<T>(Seq T, T): bool; let contains (#ty: Type) (s: seq ty) (v: ty) : Type0 = FLT.memP v s /// We represent the Dafny function `Seq#Take` with `take`: /// /// function Seq#Take<T>(s: Seq T, howMany: int): Seq T; let take (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let result, _ = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Drop` with `drop`: /// /// function Seq#Drop<T>(s: Seq T, howMany: int): Seq T; let drop (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let _, result = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Equal` with `equal`. /// /// function Seq#Equal<T>(Seq T, Seq T): bool; let equal (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 == length s1 /\ (forall j.{:pattern index s0 j \/ index s1 j} 0 <= j && j < length s0 ==> index s0 j == index s1 j) /// Instead of representing the Dafny function `Seq#SameUntil`, which /// is only ever used in Dafny to represent prefix relations, we /// instead use `is_prefix`. /// /// function Seq#SameUntil<T>(Seq T, Seq T, int): bool; let is_prefix (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 <= length s1 /\ (forall (j: nat).{:pattern index s0 j \/ index s1 j} j < length s0 ==> index s0 j == index s1 j) /// We represent the Dafny function `Seq#Rank` with `rank`. /// /// function Seq#Rank<T>(Seq T): int; let rank (#ty: Type) (v: ty) = v /// We now prove each of the facts that comprise `all_seq_facts`. /// For fact `xxx_fact`, we prove it with `xxx_lemma`. Sometimes, that /// requires a helper lemma, which we call `xxx_helper`. In some cases, /// we need multiple helpers, so we suffix their names with integers. private let length_of_empty_is_zero_lemma () : Lemma (length_of_empty_is_zero_fact) = () private let length_zero_implies_empty_lemma () : Lemma (length_zero_implies_empty_fact) = () private let singleton_length_one_lemma () : Lemma (singleton_length_one_fact) = () private let build_increments_length_lemma () : Lemma (build_increments_length_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty). length (build s v) = 1 + length s with ( FLT.append_length s [v] ) private let rec index_into_build_helper (#ty: Type) (s: list ty) (v: ty) (i: nat{i < length (append s [v])}) : Lemma (requires i <= length s) (ensures index (append s [v]) i == (if i = length s then v else index s i)) = FLT.append_length s [v]; match s with | [] -> () | hd :: tl -> if i = 0 then () else index_into_build_helper tl v (i - 1) private let index_into_build_lemma () : Lemma (requires build_increments_length_fact u#a) (ensures index_into_build_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (v: ty) (i: nat{i < length (build s v)}). (i = length s ==> index (build s v) i == v) /\ (i <> length s ==> index (build s v) i == index s i) with ( index_into_build_helper u#a s v i ) private let append_sums_lengths_lemma () : Lemma (append_sums_lengths_fact) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty). length (append s0 s1) = length s0 + length s1 with ( FLT.append_length s0 s1 ) private let index_into_singleton_lemma (_: squash (singleton_length_one_fact u#a)) : Lemma (index_into_singleton_fact u#a ()) = () private let rec index_after_append_helper (ty: Type) (s0: list ty) (s1: list ty) (n: nat) : Lemma (requires n < length (append s0 s1) && length (append s0 s1) = length s0 + length s1) (ensures index (append s0 s1) n == (if n < length s0 then index s0 n else index s1 (n - length s0))) = match s0 with | [] -> () | hd :: tl -> if n = 0 then () else index_after_append_helper ty tl s1 (n - 1) private let index_after_append_lemma (_: squash (append_sums_lengths_fact u#a)) : Lemma (index_after_append_fact u#a ()) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty) (n: nat{n < length (append s0 s1)}). (n < length s0 ==> index (append s0 s1) n == index s0 n) /\ (length s0 <= n ==> index (append s0 s1) n == index s1 (n - length s0)) with ( index_after_append_helper ty s0 s1 n ) private let rec lemma_splitAt_fst_length (#a:Type) (n:nat) (l:list a) : Lemma (requires (n <= length l)) (ensures (length (fst (FLT.splitAt n l)) = n)) = match n, l with | 0, _ -> () | _, [] -> () | _, _ :: l' -> lemma_splitAt_fst_length (n - 1) l' private let update_maintains_length_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) : Lemma (length (update s i v) = length s) = let s1, _, s2 = FLT.split3 s i in lemma_splitAt_fst_length i s; FLT.lemma_splitAt_snd_length i s; FLT.append_length [v] s2; FLT.append_length s1 (append [v] s2) private let update_maintains_length_lemma () : Lemma (update_maintains_length_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty). length (update s i v) = length s with ( update_maintains_length_helper s i v ) private let rec update_then_index_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}) : Lemma (requires n < length s) (ensures index (update s i v) n == (if i = n then v else index s n)) = match s with | hd :: tl -> if i = 0 || n = 0 then () else update_then_index_helper tl (i - 1) v (n - 1) private let update_then_index_lemma () : Lemma (update_then_index_fact) = update_maintains_length_lemma (); introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}). n < length s ==> (i = n ==> index (update s i v) n == v) /\ (i <> n ==> index (update s i v) n == index s n) with introduce _ ==> _ with given_antecedent. ( update_then_index_helper s i v n ) private let contains_iff_exists_index_lemma () : Lemma (contains_iff_exists_index_fact) = introduce forall (ty: Type) (s: seq ty) (x: ty). contains s x <==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with ( introduce contains s x ==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with given_antecedent. ( introduce exists (i: nat). i < length s /\ index s i == x with (FLT.index_of s x) and () ); introduce (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) ==> contains s x with given_antecedent. ( eliminate exists (i: nat). i < length s /\ index s i == x returns _ with _. FLT.lemma_index_memP s i ) ) private let empty_doesnt_contain_anything_lemma () : Lemma (empty_doesnt_contain_anything_fact) = () private let rec build_contains_equiv_helper (ty: Type) (s: list ty) (v: ty) (x: ty) : Lemma (FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s)) = match s with | [] -> () | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s) with _. () and _. build_contains_equiv_helper ty tl v x private let build_contains_equiv_lemma () : Lemma (build_contains_equiv_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty) (x: ty). contains (build s v) x <==> (v == x \/ contains s x) with ( build_contains_equiv_helper ty s v x ) private let rec take_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (take s n)) (ensures (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with case_x_eq_hd. assert(index s 0 == x) and case_x_ne_hd. ( take_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). i_tl < n - 1 /\ i_tl < length tl /\ index tl i_tl == x returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with _. introduce exists (i: nat). i < n /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let rec take_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (i < n /\ i < length s /\ index s i == x)) (ensures FLT.memP x (take s n)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (take s n) with case_x_eq_hd. () and case_x_ne_hd. take_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1) private let take_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (take s n) <==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = introduce FLT.memP x (take s n) ==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) with given_antecedent. (take_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) ==> FLT.memP x (take s n) with given_antecedent. ( eliminate exists (i: nat). i < n /\ i < length s /\ index s i == x returns _ with _. take_contains_equiv_exists_helper2 ty s n x i ) private let take_contains_equiv_exists_lemma () : Lemma (take_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (take s n) x <==> (exists (i: nat). i < n /\ i < length s /\ index s i == x) with ( take_contains_equiv_exists_helper3 ty s n x ) #push-options "--z3rlimit_factor 10 --fuel 1 --ifuel 1" private let rec drop_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (drop s n)) (ensures (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x with case_n_eq_0. ( eliminate x == hd \/ ~(x == hd) returns _ with _. assert(index s 0 == x) and _. ( drop_contains_equiv_exists_helper1 ty tl n x; eliminate exists (i_tl: nat). (n <= i_tl /\ i_tl < length tl /\ index tl i_tl == x) returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and () )) and case_n_ne_0. ( drop_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) #pop-options private let rec drop_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (n <= i /\ i < length s /\ index s i == x)) (ensures FLT.memP x (drop s n)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns FLT.memP x (drop s n) with _. FLT.lemma_index_memP s i and _. ( drop_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1); eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let drop_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (drop s n) <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = introduce FLT.memP x (drop s n) ==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with given_antecedent. ( drop_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) ==> FLT.memP x (drop s n) with given_antecedent. ( eliminate exists (i: nat). n <= i /\ i < length s /\ index s i == x returns _ with _. drop_contains_equiv_exists_helper2 ty s n x i ) private let drop_contains_equiv_exists_lemma () : Lemma (drop_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (drop s n) x <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with ( drop_contains_equiv_exists_helper3 ty s n x; assert (FLT.memP x (drop s n) <==> (exists (i: nat). n <= i /\ i < length s /\ index s i == x)) ) private let equal_def_lemma () : Lemma (equal_def_fact) = () private let extensionality_lemma () : Lemma (extensionality_fact) = introduce forall (ty: Type) (a: seq ty) (b: seq ty). equal a b ==> a == b with introduce _ ==> _ with given_antecedent. ( introduce forall (i: nat) . i < length a ==> index a i == index b i with introduce _ ==> _ with given_antecedent. ( assert (index a i == index b i) // needed to trigger ); FStar.List.Tot.Properties.index_extensionality a b ) private let is_prefix_def_lemma () : Lemma (is_prefix_def_fact) = () private let take_length_lemma () : Lemma (take_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (take s n) = n with introduce _ ==> _ with given_antecedent. ( lemma_splitAt_fst_length n s ) private let rec index_into_take_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < n && n <= length s /\ length (take s n) = n) (ensures index (take s n) j == index s j) = match s with | hd :: tl -> if j = 0 || n = 0 then () else index_into_take_helper tl (n - 1) (j - 1) private let index_into_take_lemma () : Lemma (requires take_length_fact u#a) (ensures index_into_take_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < n && n <= length s ==> index (take s n) j == index s j with introduce _ ==> _ with given_antecedent. ( assert (length (take s n) == n); // triggers take_length_fact index_into_take_helper s n j ) private let drop_length_lemma () : Lemma (drop_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (drop s n) = length s - n with introduce _ ==> _ with given_antecedent. ( FLT.lemma_splitAt_snd_length n s ) private let rec index_into_drop_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < length s - n /\ length (drop s n) = length s - n) (ensures index (drop s n) j == index s (j + n)) = match s with | hd :: tl -> if n = 0 then () else index_into_drop_helper tl (n - 1) j private let index_into_drop_lemma () : Lemma (requires drop_length_fact u#a) (ensures index_into_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < length s - n ==> index (drop s n) j == index s (j + n) with introduce _ ==> _ with given_antecedent. ( assert (length (drop s n) = length s - n); // triggers drop_length_fact index_into_drop_helper s n j ) private let drop_index_offset_lemma () : Lemma (requires drop_length_fact u#a) (ensures drop_index_offset_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (k: nat). n <= k && k < length s ==> index (drop s n) (k - n) == index s k with introduce _ ==> _ with given_antecedent. ( assert (length (drop s n) = length s - n); // triggers drop_length_fact index_into_drop_helper s n (k - n) ) private let rec append_then_take_or_drop_helper (#ty: Type) (s: list ty) (t: list ty) (n: nat) : Lemma (requires n = length s /\ length (append s t) = length s + length t) (ensures take (append s t) n == s /\ drop (append s t) n == t) = match s with | [] -> () | hd :: tl -> append_then_take_or_drop_helper tl t (n - 1) private let append_then_take_or_drop_lemma () : Lemma (requires append_sums_lengths_fact u#a) (ensures append_then_take_or_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (t: seq ty) (n: nat). n = length s ==> take (append s t) n == s /\ drop (append s t) n == t with introduce _ ==> _ with given_antecedent. ( append_then_take_or_drop_helper s t n ) #push-options "--z3rlimit 20" private let rec take_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s /\ length (take s n) = n) (ensures take (update s i v) n == update (take s n) i v) = match s with | hd :: tl -> if i = 0 then () else (update_maintains_length_lemma() ; take_commutes_with_in_range_update_helper tl (i - 1) v (n - 1)) #pop-options private let take_commutes_with_in_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a /\ take_length_fact u#a) (ensures take_commutes_with_in_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). i < n && n <= length s ==> take (update s i v) n == update (take s n) i v with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact assert (length (take s n) = n); // triggers take_length_fact take_commutes_with_in_range_update_helper s i v n ) private let rec take_ignores_out_of_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s) (ensures take (update s i v) n == take s n) = match s with | hd :: tl -> if n = 0 then () else take_ignores_out_of_range_update_helper tl (i - 1) v (n - 1) private let take_ignores_out_of_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a) (ensures take_ignores_out_of_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). n <= i && i < length s ==> take (update s i v) n == take s n with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact take_ignores_out_of_range_update_helper s i v n ) #push-options "--fuel 2 --ifuel 1 --z3rlimit_factor 4" private let rec drop_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s /\ length (drop s n) = length s - n) (ensures drop (update s i v) n == update (drop s n) (i - n) v) = match s with | hd :: tl -> if n = 0 then () else ( update_maintains_length_lemma (); drop_length_lemma (); drop_commutes_with_in_range_update_helper tl (i - 1) v (n - 1) ) #pop-options private let drop_commutes_with_in_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a /\ drop_length_fact u#a) (ensures drop_commutes_with_in_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). n <= i && i < length s ==> drop (update s i v) n == update (drop s n) (i - n) v with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact assert (length (drop s n) = length s - n); // triggers drop_length_fact drop_commutes_with_in_range_update_helper s i v n ) private let rec drop_ignores_out_of_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s) (ensures drop (update s i v) n == drop s n) = match s with | hd :: tl -> if i = 0 then () else drop_ignores_out_of_range_update_helper tl (i - 1) v (n - 1) private let drop_ignores_out_of_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a) (ensures drop_ignores_out_of_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). i < n && n <= length s ==> drop (update s i v) n == drop s n with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact drop_ignores_out_of_range_update_helper s i v n ) private let rec drop_commutes_with_build_helper (#ty: Type) (s: list ty) (v: ty) (n: nat) : Lemma (requires n <= length s /\ length (append s [v]) = 1 + length s) (ensures drop (append s [v]) n == append (drop s n) [v]) = match s with | [] -> assert (append s [v] == [v]); assert (n == 0); () | hd :: tl -> if n = 0 then () else drop_commutes_with_build_helper tl v (n - 1) private let drop_commutes_with_build_lemma () : Lemma (requires build_increments_length_fact u#a) (ensures drop_commutes_with_build_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (v: ty) (n: nat). n <= length s ==> drop (build s v) n == build (drop s n) v with introduce _ ==> _ with given_antecedent. ( assert (length (build s v) = 1 + length s); // triggers build_increments_length_fact drop_commutes_with_build_helper s v n ) private let rank_def_lemma () : Lemma (rank_def_fact) = () private let element_ranks_less_lemma () : Lemma (element_ranks_less_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat). i < length s ==> rank (index s i) << rank s with introduce _ ==> _ with given_antecedent. ( contains_iff_exists_index_lemma (); assert (contains s (index s i)); FLT.memP_precedes (index s i) s ) private let rec drop_ranks_less_helper (ty: Type) (s: list ty) (i: nat) : Lemma (requires 0 < i && i <= length s) (ensures drop s i << s) = match s with | [] -> () | hd :: tl -> if i = 1 then () else drop_ranks_less_helper ty tl (i - 1) private let drop_ranks_less_lemma () : Lemma (drop_ranks_less_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat). 0 < i && i <= length s ==> rank (drop s i) << rank s with introduce _ ==> _ with given_antecedent. ( drop_ranks_less_helper ty s i ) private let take_ranks_less_lemma () : Lemma (take_ranks_less_fact) = take_length_lemma () private let append_take_drop_ranks_less_lemma () : Lemma (append_take_drop_ranks_less_fact) = take_length_lemma (); drop_length_lemma (); append_sums_lengths_lemma () private let drop_zero_lemma () : Lemma (drop_zero_fact) = () private let take_zero_lemma () : Lemma (take_zero_fact) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Properties.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked" ], "interface_file": true, "source_file": "FStar.Sequence.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "FLT" }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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.Sequence.Base.seq ty -> m: Prims.nat -> n: Prims.nat -> FStar.Pervasives.Lemma (requires m + n <= FStar.Sequence.Base.length s /\ FStar.Sequence.Base.length (FStar.Sequence.Base.drop s m) = FStar.Sequence.Base.length s - m ) (ensures FStar.Sequence.Base.drop (FStar.Sequence.Base.drop s m) n == FStar.Sequence.Base.drop s (m + n))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Sequence.Base.seq", "Prims.nat", "Prims.list", "Prims.op_Equality", "Prims.int", "Prims.bool", "FStar.Sequence.Base.drop_then_drop_helper", "Prims.op_Subtraction", "Prims.unit", "FStar.Sequence.Base.drop_length_lemma", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Sequence.Base.length", "FStar.Sequence.Base.drop", "Prims.squash", "Prims.eq2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec drop_then_drop_helper (#ty: Type) (s: seq ty) (m n: nat) : Lemma (requires m + n <= length s /\ length (drop s m) = length s - m) (ensures drop (drop s m) n == drop s (m + n)) =
match s with | [] -> () | hd :: tl -> if m = 0 then () else (drop_length_lemma (); drop_then_drop_helper tl (m - 1) n)
false
AlgWP.fst
AlgWP.st_wp
val st_wp : a: Type -> Type
let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp}
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 49, "end_line": 57, "start_col": 0, "start_line": 57 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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 -> Type
Prims.Tot
[ "total" ]
[]
[ "AlgWP.st_wp0", "AlgWP.st_monotonic" ]
[]
false
false
false
true
true
let st_wp (a: Type) =
wp: st_wp0 a {st_monotonic wp}
false
AlgWP.fst
AlgWP.st_monotonic
val st_monotonic (#a: _) (w: st_wp0 a) : Type0
val st_monotonic (#a: _) (w: st_wp0 a) : Type0
let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 83, "end_line": 55, "start_col": 0, "start_line": 52 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
w: AlgWP.st_wp0 a -> Type0
Prims.Tot
[ "total" ]
[]
[ "AlgWP.st_wp0", "Prims.l_Forall", "Alg.state", "FStar.Pervasives.Native.tuple2", "Prims.logical", "Prims.l_imp", "FStar.Pervasives.Native.Mktuple2" ]
[]
false
false
false
true
true
let st_monotonic #a (w: st_wp0 a) : Type0 =
forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2
false
AlgWP.fst
AlgWP.tbind
val tbind: #a: _ -> #b: _ -> #labs1: _ -> #labs2: _ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1 @@ labs2)
val tbind: #a: _ -> #b: _ -> #labs1: _ -> #labs2: _ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1 @@ labs2)
let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 90, "end_line": 48, "start_col": 0, "start_line": 45 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: AlgWP.rwtree a labs1 -> f: (_: a -> AlgWP.rwtree b labs2) -> AlgWP.rwtree b (labs1 @@ labs2)
Prims.Tot
[ "total" ]
[]
[ "Alg.ops", "Alg.sublist", "Prims.Cons", "Alg.op", "Alg.Read", "Alg.Write", "Prims.Nil", "AlgWP.rwtree", "Alg.bind", "AlgWP.op_At_At" ]
[]
false
false
false
false
false
let tbind: #a: _ -> #b: _ -> #labs1: _ -> #labs2: _ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1 @@ labs2) =
fun c f -> Alg.bind _ _ c f
false
Pulse.Recursion.fst
Pulse.Recursion.map2
val map2 (#a #b #c: _) (f: (a -> b -> Tac c)) (xs: list a) (ys: list b) : Tac (list c)
val map2 (#a #b #c: _) (f: (a -> b -> Tac c)) (xs: list a) (ys: list b) : Tac (list c)
let rec map2 #a #b #c (f : a -> b -> Tac c) (xs : list a) (ys : list b) : Tac (list c) = match xs, ys with | [], [] -> [] | x::xx, y::yy -> f x y :: map2 f xx yy | _ -> raise Map2_length_mismatch
{ "file_name": "lib/steel/pulse/Pulse.Recursion.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 35, "end_line": 46, "start_col": 0, "start_line": 42 }
(* 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.Recursion module R = FStar.Reflection.V2 module RT = FStar.Reflection.Typing module T = FStar.Tactics.V2 open FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open FStar.List.Tot open Pulse.Syntax open Pulse.Typing module P = Pulse.Syntax.Printer exception Splitlast_empty let rec splitlast #a (l : list a) : Tac (list a & a) = match l with | [] -> raise Splitlast_empty | [x] -> [], x | x::xs -> let init, last = splitlast xs in x::init, last exception Map2_length_mismatch
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.PP.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.SyntaxHelpers.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.NamedView.fsti.checked", "FStar.Stubs.Syntax.Syntax.fsti.checked", "FStar.Stubs.Reflection.V2.Builtins.fsti.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Reflection.Typing.fsti.checked", "FStar.Printf.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked" ], "interface_file": true, "source_file": "Pulse.Recursion.fst" }
[ { "abbrev": true, "full_module": "Pulse.Syntax.Printer", "short_module": "P" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "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
f: (_: a -> _: b -> FStar.Tactics.Effect.Tac c) -> xs: Prims.list a -> ys: Prims.list b -> FStar.Tactics.Effect.Tac (Prims.list c)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.list", "FStar.Pervasives.Native.Mktuple2", "Prims.Nil", "Prims.Cons", "Pulse.Recursion.map2", "FStar.Pervasives.Native.tuple2", "FStar.Tactics.Effect.raise", "Pulse.Recursion.Map2_length_mismatch" ]
[ "recursion" ]
false
true
false
false
false
let rec map2 #a #b #c (f: (a -> b -> Tac c)) (xs: list a) (ys: list b) : Tac (list c) =
match xs, ys with | [], [] -> [] | x :: xx, y :: yy -> f x y :: map2 f xx yy | _ -> raise Map2_length_mismatch
false
Pulse.Recursion.fst
Pulse.Recursion.subst_binder_typ
val subst_binder_typ (s: FStar.Stubs.Syntax.Syntax.subst_t) (b: Tactics.NamedView.binder) : Tactics.NamedView.binder
val subst_binder_typ (s: FStar.Stubs.Syntax.Syntax.subst_t) (b: Tactics.NamedView.binder) : Tactics.NamedView.binder
let subst_binder_typ (s : FStar.Stubs.Syntax.Syntax.subst_t) (b : Tactics.NamedView.binder) : Tactics.NamedView.binder = { b with sort = FStar.Stubs.Reflection.V2.Builtins.subst_term s b.sort }
{ "file_name": "lib/steel/pulse/Pulse.Recursion.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 74, "end_line": 61, "start_col": 0, "start_line": 60 }
(* 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.Recursion module R = FStar.Reflection.V2 module RT = FStar.Reflection.Typing module T = FStar.Tactics.V2 open FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open FStar.List.Tot open Pulse.Syntax open Pulse.Typing module P = Pulse.Syntax.Printer exception Splitlast_empty let rec splitlast #a (l : list a) : Tac (list a & a) = match l with | [] -> raise Splitlast_empty | [x] -> [], x | x::xs -> let init, last = splitlast xs in x::init, last exception Map2_length_mismatch let rec map2 #a #b #c (f : a -> b -> Tac c) (xs : list a) (ys : list b) : Tac (list c) = match xs, ys with | [], [] -> [] | x::xx, y::yy -> f x y :: map2 f xx yy | _ -> raise Map2_length_mismatch let debug_main g (s: unit -> Tac string) : Tac unit = if RU.debug_at_level (fstar_env g) "pulse.main" then print (s ()) else () let string_as_term (s:string) : R.term = R.pack_ln (R.Tv_Const (C_String s)) let freshen_binder (b:T.binder) : T.binder = { b with uniq = 10000 + b.uniq ; ppname = map_seal b.ppname (fun s -> s ^ "'") }
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.PP.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.SyntaxHelpers.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.NamedView.fsti.checked", "FStar.Stubs.Syntax.Syntax.fsti.checked", "FStar.Stubs.Reflection.V2.Builtins.fsti.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Reflection.Typing.fsti.checked", "FStar.Printf.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked" ], "interface_file": true, "source_file": "Pulse.Recursion.fst" }
[ { "abbrev": true, "full_module": "Pulse.Syntax.Printer", "short_module": "P" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "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
s: FStar.Stubs.Syntax.Syntax.subst_t -> b: FStar.Tactics.NamedView.binder -> FStar.Tactics.NamedView.binder
Prims.Tot
[ "total" ]
[]
[ "FStar.Stubs.Syntax.Syntax.subst_t", "FStar.Tactics.NamedView.binder", "FStar.Tactics.NamedView.Mkbinder", "FStar.Tactics.NamedView.__proj__Mkbinder__item__uniq", "FStar.Tactics.NamedView.__proj__Mkbinder__item__ppname", "FStar.Stubs.Reflection.V2.Builtins.subst_term", "FStar.Tactics.NamedView.__proj__Mkbinder__item__sort", "FStar.Tactics.NamedView.__proj__Mkbinder__item__qual", "FStar.Tactics.NamedView.__proj__Mkbinder__item__attrs" ]
[]
false
false
false
true
false
let subst_binder_typ (s: FStar.Stubs.Syntax.Syntax.subst_t) (b: Tactics.NamedView.binder) : Tactics.NamedView.binder =
{ b with sort = FStar.Stubs.Reflection.V2.Builtins.subst_term s b.sort }
false
Pulse.Recursion.fst
Pulse.Recursion.string_as_term
val string_as_term (s: string) : R.term
val string_as_term (s: string) : R.term
let string_as_term (s:string) : R.term = R.pack_ln (R.Tv_Const (C_String s))
{ "file_name": "lib/steel/pulse/Pulse.Recursion.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 37, "end_line": 54, "start_col": 0, "start_line": 53 }
(* 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.Recursion module R = FStar.Reflection.V2 module RT = FStar.Reflection.Typing module T = FStar.Tactics.V2 open FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open FStar.List.Tot open Pulse.Syntax open Pulse.Typing module P = Pulse.Syntax.Printer exception Splitlast_empty let rec splitlast #a (l : list a) : Tac (list a & a) = match l with | [] -> raise Splitlast_empty | [x] -> [], x | x::xs -> let init, last = splitlast xs in x::init, last exception Map2_length_mismatch let rec map2 #a #b #c (f : a -> b -> Tac c) (xs : list a) (ys : list b) : Tac (list c) = match xs, ys with | [], [] -> [] | x::xx, y::yy -> f x y :: map2 f xx yy | _ -> raise Map2_length_mismatch let debug_main g (s: unit -> Tac string) : Tac unit = if RU.debug_at_level (fstar_env g) "pulse.main" then print (s ()) else ()
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.PP.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.SyntaxHelpers.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.NamedView.fsti.checked", "FStar.Stubs.Syntax.Syntax.fsti.checked", "FStar.Stubs.Reflection.V2.Builtins.fsti.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Reflection.Typing.fsti.checked", "FStar.Printf.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked" ], "interface_file": true, "source_file": "Pulse.Recursion.fst" }
[ { "abbrev": true, "full_module": "Pulse.Syntax.Printer", "short_module": "P" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "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
s: Prims.string -> FStar.Stubs.Reflection.Types.term
Prims.Tot
[ "total" ]
[]
[ "Prims.string", "FStar.Stubs.Reflection.V2.Builtins.pack_ln", "FStar.Stubs.Reflection.V2.Data.Tv_Const", "FStar.Stubs.Reflection.V2.Data.C_String", "FStar.Stubs.Reflection.Types.term" ]
[]
false
false
false
true
false
let string_as_term (s: string) : R.term =
R.pack_ln (R.Tv_Const (C_String s))
false
FStar.Sequence.Base.fst
FStar.Sequence.Base.drop_commutes_with_in_range_update_helper
val drop_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s /\ length (drop s n) = length s - n) (ensures drop (update s i v) n == update (drop s n) (i - n) v)
val drop_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s /\ length (drop s n) = length s - n) (ensures drop (update s i v) n == update (drop s n) (i - n) v)
let rec drop_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s /\ length (drop s n) = length s - n) (ensures drop (update s i v) n == update (drop s n) (i - n) v) = match s with | hd :: tl -> if n = 0 then () else ( update_maintains_length_lemma (); drop_length_lemma (); drop_commutes_with_in_range_update_helper tl (i - 1) v (n - 1) )
{ "file_name": "ulib/experimental/FStar.Sequence.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 582, "start_col": 8, "start_line": 567 }
(* Copyright 2008-2021 Jay Lorch, Rustan Leino, Alex Summers, Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to the Dafny Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Includes material from the Dafny project (https://github.com/dafny-lang/dafny) which carries this license information: Created 9 February 2008 by Rustan Leino. Converted to Boogie 2 on 28 June 2008. Edited sequence axioms 20 October 2009 by Alex Summers. Modified 2014 by Dan Rosen. Copyright (c) 2008-2014, Microsoft. Copyright by the contributors to the Dafny Project SPDX-License-Identifier: MIT *) (** This module declares a type and functions used for modeling sequences as they're modeled in Dafny. It also states and proves some properties about sequences, and provides a lemma `all_seq_facts_lemma` one can call to bring them into context. The properties are modeled after those in the Dafny sequence axioms, with patterns for quantifiers chosen as in those axioms. @summary Type, functions, and properties of sequences *) module FStar.Sequence.Base module FLT = FStar.List.Tot /// Internally, we represent a sequence as a list. type seq (ty: Type) = list ty /// We represent the Dafny function `Seq#Length` with `length`: /// /// function Seq#Length<T>(Seq T): int; let length = FLT.length /// We represent the Dafny function `Seq#Empty` with `empty`: /// /// function Seq#Empty<T>(): Seq T; let empty (#ty: Type) : seq ty = [] /// We represent the Dafny function `Seq#Singleton` with `singleton`: /// /// function Seq#Singleton<T>(T): Seq T; let singleton (#ty: Type) (v: ty) : seq ty = [v] /// We represent the Dafny function `Seq#Index` with `index`: /// /// function Seq#Index<T>(Seq T, int): T; let index (#ty: Type) (s: seq ty) (i: nat{i < length s}) : ty = FLT.index s i /// We represent the Dafny function `Seq#Build` with `build`: /// /// function Seq#Build<T>(s: Seq T, val: T): Seq T; let build (#ty: Type) (s: seq ty) (v: ty) : seq ty = FLT.append s [v] /// We represent the Dafny function `Seq#Append` with `append`: /// /// function Seq#Append<T>(Seq T, Seq T): Seq T; let append = FLT.append /// We represent the Dafny function `Seq#Update` with `update`: /// /// function Seq#Update<T>(Seq T, int, T): Seq T; let update (#ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) : seq ty = let s1, _, s2 = FLT.split3 s i in append s1 (append [v] s2) /// We represent the Dafny function `Seq#Contains` with `contains`: /// /// function Seq#Contains<T>(Seq T, T): bool; let contains (#ty: Type) (s: seq ty) (v: ty) : Type0 = FLT.memP v s /// We represent the Dafny function `Seq#Take` with `take`: /// /// function Seq#Take<T>(s: Seq T, howMany: int): Seq T; let take (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let result, _ = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Drop` with `drop`: /// /// function Seq#Drop<T>(s: Seq T, howMany: int): Seq T; let drop (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let _, result = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Equal` with `equal`. /// /// function Seq#Equal<T>(Seq T, Seq T): bool; let equal (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 == length s1 /\ (forall j.{:pattern index s0 j \/ index s1 j} 0 <= j && j < length s0 ==> index s0 j == index s1 j) /// Instead of representing the Dafny function `Seq#SameUntil`, which /// is only ever used in Dafny to represent prefix relations, we /// instead use `is_prefix`. /// /// function Seq#SameUntil<T>(Seq T, Seq T, int): bool; let is_prefix (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 <= length s1 /\ (forall (j: nat).{:pattern index s0 j \/ index s1 j} j < length s0 ==> index s0 j == index s1 j) /// We represent the Dafny function `Seq#Rank` with `rank`. /// /// function Seq#Rank<T>(Seq T): int; let rank (#ty: Type) (v: ty) = v /// We now prove each of the facts that comprise `all_seq_facts`. /// For fact `xxx_fact`, we prove it with `xxx_lemma`. Sometimes, that /// requires a helper lemma, which we call `xxx_helper`. In some cases, /// we need multiple helpers, so we suffix their names with integers. private let length_of_empty_is_zero_lemma () : Lemma (length_of_empty_is_zero_fact) = () private let length_zero_implies_empty_lemma () : Lemma (length_zero_implies_empty_fact) = () private let singleton_length_one_lemma () : Lemma (singleton_length_one_fact) = () private let build_increments_length_lemma () : Lemma (build_increments_length_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty). length (build s v) = 1 + length s with ( FLT.append_length s [v] ) private let rec index_into_build_helper (#ty: Type) (s: list ty) (v: ty) (i: nat{i < length (append s [v])}) : Lemma (requires i <= length s) (ensures index (append s [v]) i == (if i = length s then v else index s i)) = FLT.append_length s [v]; match s with | [] -> () | hd :: tl -> if i = 0 then () else index_into_build_helper tl v (i - 1) private let index_into_build_lemma () : Lemma (requires build_increments_length_fact u#a) (ensures index_into_build_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (v: ty) (i: nat{i < length (build s v)}). (i = length s ==> index (build s v) i == v) /\ (i <> length s ==> index (build s v) i == index s i) with ( index_into_build_helper u#a s v i ) private let append_sums_lengths_lemma () : Lemma (append_sums_lengths_fact) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty). length (append s0 s1) = length s0 + length s1 with ( FLT.append_length s0 s1 ) private let index_into_singleton_lemma (_: squash (singleton_length_one_fact u#a)) : Lemma (index_into_singleton_fact u#a ()) = () private let rec index_after_append_helper (ty: Type) (s0: list ty) (s1: list ty) (n: nat) : Lemma (requires n < length (append s0 s1) && length (append s0 s1) = length s0 + length s1) (ensures index (append s0 s1) n == (if n < length s0 then index s0 n else index s1 (n - length s0))) = match s0 with | [] -> () | hd :: tl -> if n = 0 then () else index_after_append_helper ty tl s1 (n - 1) private let index_after_append_lemma (_: squash (append_sums_lengths_fact u#a)) : Lemma (index_after_append_fact u#a ()) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty) (n: nat{n < length (append s0 s1)}). (n < length s0 ==> index (append s0 s1) n == index s0 n) /\ (length s0 <= n ==> index (append s0 s1) n == index s1 (n - length s0)) with ( index_after_append_helper ty s0 s1 n ) private let rec lemma_splitAt_fst_length (#a:Type) (n:nat) (l:list a) : Lemma (requires (n <= length l)) (ensures (length (fst (FLT.splitAt n l)) = n)) = match n, l with | 0, _ -> () | _, [] -> () | _, _ :: l' -> lemma_splitAt_fst_length (n - 1) l' private let update_maintains_length_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) : Lemma (length (update s i v) = length s) = let s1, _, s2 = FLT.split3 s i in lemma_splitAt_fst_length i s; FLT.lemma_splitAt_snd_length i s; FLT.append_length [v] s2; FLT.append_length s1 (append [v] s2) private let update_maintains_length_lemma () : Lemma (update_maintains_length_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty). length (update s i v) = length s with ( update_maintains_length_helper s i v ) private let rec update_then_index_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}) : Lemma (requires n < length s) (ensures index (update s i v) n == (if i = n then v else index s n)) = match s with | hd :: tl -> if i = 0 || n = 0 then () else update_then_index_helper tl (i - 1) v (n - 1) private let update_then_index_lemma () : Lemma (update_then_index_fact) = update_maintains_length_lemma (); introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}). n < length s ==> (i = n ==> index (update s i v) n == v) /\ (i <> n ==> index (update s i v) n == index s n) with introduce _ ==> _ with given_antecedent. ( update_then_index_helper s i v n ) private let contains_iff_exists_index_lemma () : Lemma (contains_iff_exists_index_fact) = introduce forall (ty: Type) (s: seq ty) (x: ty). contains s x <==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with ( introduce contains s x ==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with given_antecedent. ( introduce exists (i: nat). i < length s /\ index s i == x with (FLT.index_of s x) and () ); introduce (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) ==> contains s x with given_antecedent. ( eliminate exists (i: nat). i < length s /\ index s i == x returns _ with _. FLT.lemma_index_memP s i ) ) private let empty_doesnt_contain_anything_lemma () : Lemma (empty_doesnt_contain_anything_fact) = () private let rec build_contains_equiv_helper (ty: Type) (s: list ty) (v: ty) (x: ty) : Lemma (FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s)) = match s with | [] -> () | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s) with _. () and _. build_contains_equiv_helper ty tl v x private let build_contains_equiv_lemma () : Lemma (build_contains_equiv_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty) (x: ty). contains (build s v) x <==> (v == x \/ contains s x) with ( build_contains_equiv_helper ty s v x ) private let rec take_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (take s n)) (ensures (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with case_x_eq_hd. assert(index s 0 == x) and case_x_ne_hd. ( take_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). i_tl < n - 1 /\ i_tl < length tl /\ index tl i_tl == x returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with _. introduce exists (i: nat). i < n /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let rec take_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (i < n /\ i < length s /\ index s i == x)) (ensures FLT.memP x (take s n)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (take s n) with case_x_eq_hd. () and case_x_ne_hd. take_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1) private let take_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (take s n) <==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = introduce FLT.memP x (take s n) ==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) with given_antecedent. (take_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) ==> FLT.memP x (take s n) with given_antecedent. ( eliminate exists (i: nat). i < n /\ i < length s /\ index s i == x returns _ with _. take_contains_equiv_exists_helper2 ty s n x i ) private let take_contains_equiv_exists_lemma () : Lemma (take_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (take s n) x <==> (exists (i: nat). i < n /\ i < length s /\ index s i == x) with ( take_contains_equiv_exists_helper3 ty s n x ) #push-options "--z3rlimit_factor 10 --fuel 1 --ifuel 1" private let rec drop_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (drop s n)) (ensures (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x with case_n_eq_0. ( eliminate x == hd \/ ~(x == hd) returns _ with _. assert(index s 0 == x) and _. ( drop_contains_equiv_exists_helper1 ty tl n x; eliminate exists (i_tl: nat). (n <= i_tl /\ i_tl < length tl /\ index tl i_tl == x) returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and () )) and case_n_ne_0. ( drop_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) #pop-options private let rec drop_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (n <= i /\ i < length s /\ index s i == x)) (ensures FLT.memP x (drop s n)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns FLT.memP x (drop s n) with _. FLT.lemma_index_memP s i and _. ( drop_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1); eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let drop_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (drop s n) <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = introduce FLT.memP x (drop s n) ==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with given_antecedent. ( drop_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) ==> FLT.memP x (drop s n) with given_antecedent. ( eliminate exists (i: nat). n <= i /\ i < length s /\ index s i == x returns _ with _. drop_contains_equiv_exists_helper2 ty s n x i ) private let drop_contains_equiv_exists_lemma () : Lemma (drop_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (drop s n) x <==> (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x) with ( drop_contains_equiv_exists_helper3 ty s n x; assert (FLT.memP x (drop s n) <==> (exists (i: nat). n <= i /\ i < length s /\ index s i == x)) ) private let equal_def_lemma () : Lemma (equal_def_fact) = () private let extensionality_lemma () : Lemma (extensionality_fact) = introduce forall (ty: Type) (a: seq ty) (b: seq ty). equal a b ==> a == b with introduce _ ==> _ with given_antecedent. ( introduce forall (i: nat) . i < length a ==> index a i == index b i with introduce _ ==> _ with given_antecedent. ( assert (index a i == index b i) // needed to trigger ); FStar.List.Tot.Properties.index_extensionality a b ) private let is_prefix_def_lemma () : Lemma (is_prefix_def_fact) = () private let take_length_lemma () : Lemma (take_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (take s n) = n with introduce _ ==> _ with given_antecedent. ( lemma_splitAt_fst_length n s ) private let rec index_into_take_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < n && n <= length s /\ length (take s n) = n) (ensures index (take s n) j == index s j) = match s with | hd :: tl -> if j = 0 || n = 0 then () else index_into_take_helper tl (n - 1) (j - 1) private let index_into_take_lemma () : Lemma (requires take_length_fact u#a) (ensures index_into_take_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < n && n <= length s ==> index (take s n) j == index s j with introduce _ ==> _ with given_antecedent. ( assert (length (take s n) == n); // triggers take_length_fact index_into_take_helper s n j ) private let drop_length_lemma () : Lemma (drop_length_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat). n <= length s ==> length (drop s n) = length s - n with introduce _ ==> _ with given_antecedent. ( FLT.lemma_splitAt_snd_length n s ) private let rec index_into_drop_helper (#ty: Type) (s: list ty) (n: nat) (j: nat) : Lemma (requires j < length s - n /\ length (drop s n) = length s - n) (ensures index (drop s n) j == index s (j + n)) = match s with | hd :: tl -> if n = 0 then () else index_into_drop_helper tl (n - 1) j private let index_into_drop_lemma () : Lemma (requires drop_length_fact u#a) (ensures index_into_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (j: nat). j < length s - n ==> index (drop s n) j == index s (j + n) with introduce _ ==> _ with given_antecedent. ( assert (length (drop s n) = length s - n); // triggers drop_length_fact index_into_drop_helper s n j ) private let drop_index_offset_lemma () : Lemma (requires drop_length_fact u#a) (ensures drop_index_offset_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (n: nat) (k: nat). n <= k && k < length s ==> index (drop s n) (k - n) == index s k with introduce _ ==> _ with given_antecedent. ( assert (length (drop s n) = length s - n); // triggers drop_length_fact index_into_drop_helper s n (k - n) ) private let rec append_then_take_or_drop_helper (#ty: Type) (s: list ty) (t: list ty) (n: nat) : Lemma (requires n = length s /\ length (append s t) = length s + length t) (ensures take (append s t) n == s /\ drop (append s t) n == t) = match s with | [] -> () | hd :: tl -> append_then_take_or_drop_helper tl t (n - 1) private let append_then_take_or_drop_lemma () : Lemma (requires append_sums_lengths_fact u#a) (ensures append_then_take_or_drop_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (t: seq ty) (n: nat). n = length s ==> take (append s t) n == s /\ drop (append s t) n == t with introduce _ ==> _ with given_antecedent. ( append_then_take_or_drop_helper s t n ) #push-options "--z3rlimit 20" private let rec take_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires i < n /\ n <= length s /\ length (update s i v) = length s /\ length (take s n) = n) (ensures take (update s i v) n == update (take s n) i v) = match s with | hd :: tl -> if i = 0 then () else (update_maintains_length_lemma() ; take_commutes_with_in_range_update_helper tl (i - 1) v (n - 1)) #pop-options private let take_commutes_with_in_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a /\ take_length_fact u#a) (ensures take_commutes_with_in_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). i < n && n <= length s ==> take (update s i v) n == update (take s n) i v with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact assert (length (take s n) = n); // triggers take_length_fact take_commutes_with_in_range_update_helper s i v n ) private let rec take_ignores_out_of_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s) (ensures take (update s i v) n == take s n) = match s with | hd :: tl -> if n = 0 then () else take_ignores_out_of_range_update_helper tl (i - 1) v (n - 1) private let take_ignores_out_of_range_update_lemma () : Lemma (requires update_maintains_length_fact u#a) (ensures take_ignores_out_of_range_update_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (i: nat) (v: ty) (n: nat). n <= i && i < length s ==> take (update s i v) n == take s n with introduce _ ==> _ with given_antecedent. ( assert (length (update s i v) = length s); // triggers update_maintains_length_fact take_ignores_out_of_range_update_helper s i v n )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Properties.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked" ], "interface_file": true, "source_file": "FStar.Sequence.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "FLT" }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": 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": 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": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s: Prims.list ty -> i: Prims.nat -> v: ty -> n: Prims.nat -> FStar.Pervasives.Lemma (requires n <= i /\ i < FStar.Sequence.Base.length s /\ FStar.Sequence.Base.length (FStar.Sequence.Base.update s i v) = FStar.Sequence.Base.length s /\ FStar.Sequence.Base.length (FStar.Sequence.Base.drop s n) = FStar.Sequence.Base.length s - n ) (ensures FStar.Sequence.Base.drop (FStar.Sequence.Base.update s i v) n == FStar.Sequence.Base.update (FStar.Sequence.Base.drop s n) (i - n) v)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Prims.nat", "Prims.op_Equality", "Prims.int", "Prims.bool", "FStar.Sequence.Base.drop_commutes_with_in_range_update_helper", "Prims.op_Subtraction", "Prims.unit", "FStar.Sequence.Base.drop_length_lemma", "FStar.Sequence.Base.update_maintains_length_lemma", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_LessThan", "FStar.Sequence.Base.length", "FStar.Sequence.Base.update", "FStar.Sequence.Base.drop", "Prims.squash", "Prims.eq2", "FStar.Sequence.Base.seq", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec drop_commutes_with_in_range_update_helper (#ty: Type) (s: list ty) (i: nat) (v: ty) (n: nat) : Lemma (requires n <= i /\ i < length s /\ length (update s i v) = length s /\ length (drop s n) = length s - n) (ensures drop (update s i v) n == update (drop s n) (i - n) v) =
match s with | hd :: tl -> if n = 0 then () else (update_maintains_length_lemma (); drop_length_lemma (); drop_commutes_with_in_range_update_helper tl (i - 1) v (n - 1))
false
AlgWP.fst
AlgWP.stronger
val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0
val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0
let stronger w1 w2 = forall p s. w1 p s ==> w2 p s
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 50, "end_line": 95, "start_col": 0, "start_line": 95 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
w1: AlgWP.st_wp a -> w2: AlgWP.st_wp a -> Type0
Prims.Tot
[ "total" ]
[]
[ "AlgWP.st_wp", "Prims.l_Forall", "Alg.state", "FStar.Pervasives.Native.tuple2", "Prims.l_imp" ]
[]
false
false
false
true
true
let stronger w1 w2 =
forall p s. w1 p s ==> w2 p s
false
AlgWP.fst
AlgWP.equiv
val equiv : w1: AlgWP.st_wp a -> w2: AlgWP.st_wp a -> Prims.logical
let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 69, "end_line": 97, "start_col": 0, "start_line": 97 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
w1: AlgWP.st_wp a -> w2: AlgWP.st_wp a -> Prims.logical
Prims.Tot
[ "total" ]
[]
[ "AlgWP.st_wp", "Prims.l_and", "AlgWP.stronger", "Prims.logical" ]
[]
false
false
false
true
true
let equiv #a (w1: st_wp a) (w2: st_wp a) =
w1 `stronger` w2 /\ w2 `stronger` w1
false
AlgWP.fst
AlgWP.wp_is_monotonic
val wp_is_monotonic (#a: _) (wp: st_wp a) : Type0
val wp_is_monotonic (#a: _) (wp: st_wp a) : Type0
let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 85, "end_line": 108, "start_col": 0, "start_line": 107 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
wp: AlgWP.st_wp a -> Type0
Prims.Tot
[ "total" ]
[]
[ "AlgWP.st_wp", "Prims.l_Forall", "FStar.Pervasives.Native.tuple2", "Alg.state", "Prims.logical", "Prims.l_imp", "FStar.Pervasives.Native.Mktuple2" ]
[]
false
false
false
true
true
let wp_is_monotonic #a (wp: st_wp a) : Type0 =
forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2
false
AlgWP.fst
AlgWP.sublist_at_const
val sublist_at_const (l1 l2 l3: ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1 @ l2) l3)) [SMTPat (sublist (l1 @ l2) l3)]
val sublist_at_const (l1 l2 l3: ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1 @ l2) l3)) [SMTPat (sublist (l1 @ l2) l3)]
let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 38, "end_line": 30, "start_col": 0, "start_line": 24 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
l1: Alg.ops -> l2: Alg.ops -> l3: Alg.ops -> FStar.Pervasives.Lemma (requires Alg.sublist l1 l3 /\ Alg.sublist l2 l3) (ensures Alg.sublist (l1 @ l2) l3) [SMTPat (Alg.sublist (l1 @ l2) l3)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Alg.ops", "Alg.op", "Prims.list", "AlgWP.sublist_at_const", "Prims.unit", "Prims.l_and", "Alg.sublist", "Prims.squash", "FStar.List.Tot.Base.op_At", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.logical", "Prims.Nil" ]
[ "recursion" ]
false
false
true
false
false
let rec sublist_at_const (l1 l2 l3: ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1 @ l2) l3)) [SMTPat (sublist (l1 @ l2) l3)] =
match l1 with | [] -> () | h :: t -> sublist_at_const t l2 l3
false
FStar.Sequence.Base.fst
FStar.Sequence.Base.drop_contains_equiv_exists_helper1
val drop_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (drop s n)) (ensures (exists (i: nat). {:pattern index s i} n <= i /\ i < length s /\ index s i == x))
val drop_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (drop s n)) (ensures (exists (i: nat). {:pattern index s i} n <= i /\ i < length s /\ index s i == x))
let rec drop_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (drop s n)) (ensures (exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns exists (i: nat).{:pattern index s i} n <= i /\ i < length s /\ index s i == x with case_n_eq_0. ( eliminate x == hd \/ ~(x == hd) returns _ with _. assert(index s 0 == x) and _. ( drop_contains_equiv_exists_helper1 ty tl n x; eliminate exists (i_tl: nat). (n <= i_tl /\ i_tl < length tl /\ index tl i_tl == x) returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and () )) and case_n_ne_0. ( drop_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i. n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ())
{ "file_name": "ulib/experimental/FStar.Sequence.Base.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 99, "end_line": 370, "start_col": 8, "start_line": 349 }
(* Copyright 2008-2021 Jay Lorch, Rustan Leino, Alex Summers, Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to the Dafny Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Includes material from the Dafny project (https://github.com/dafny-lang/dafny) which carries this license information: Created 9 February 2008 by Rustan Leino. Converted to Boogie 2 on 28 June 2008. Edited sequence axioms 20 October 2009 by Alex Summers. Modified 2014 by Dan Rosen. Copyright (c) 2008-2014, Microsoft. Copyright by the contributors to the Dafny Project SPDX-License-Identifier: MIT *) (** This module declares a type and functions used for modeling sequences as they're modeled in Dafny. It also states and proves some properties about sequences, and provides a lemma `all_seq_facts_lemma` one can call to bring them into context. The properties are modeled after those in the Dafny sequence axioms, with patterns for quantifiers chosen as in those axioms. @summary Type, functions, and properties of sequences *) module FStar.Sequence.Base module FLT = FStar.List.Tot /// Internally, we represent a sequence as a list. type seq (ty: Type) = list ty /// We represent the Dafny function `Seq#Length` with `length`: /// /// function Seq#Length<T>(Seq T): int; let length = FLT.length /// We represent the Dafny function `Seq#Empty` with `empty`: /// /// function Seq#Empty<T>(): Seq T; let empty (#ty: Type) : seq ty = [] /// We represent the Dafny function `Seq#Singleton` with `singleton`: /// /// function Seq#Singleton<T>(T): Seq T; let singleton (#ty: Type) (v: ty) : seq ty = [v] /// We represent the Dafny function `Seq#Index` with `index`: /// /// function Seq#Index<T>(Seq T, int): T; let index (#ty: Type) (s: seq ty) (i: nat{i < length s}) : ty = FLT.index s i /// We represent the Dafny function `Seq#Build` with `build`: /// /// function Seq#Build<T>(s: Seq T, val: T): Seq T; let build (#ty: Type) (s: seq ty) (v: ty) : seq ty = FLT.append s [v] /// We represent the Dafny function `Seq#Append` with `append`: /// /// function Seq#Append<T>(Seq T, Seq T): Seq T; let append = FLT.append /// We represent the Dafny function `Seq#Update` with `update`: /// /// function Seq#Update<T>(Seq T, int, T): Seq T; let update (#ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) : seq ty = let s1, _, s2 = FLT.split3 s i in append s1 (append [v] s2) /// We represent the Dafny function `Seq#Contains` with `contains`: /// /// function Seq#Contains<T>(Seq T, T): bool; let contains (#ty: Type) (s: seq ty) (v: ty) : Type0 = FLT.memP v s /// We represent the Dafny function `Seq#Take` with `take`: /// /// function Seq#Take<T>(s: Seq T, howMany: int): Seq T; let take (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let result, _ = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Drop` with `drop`: /// /// function Seq#Drop<T>(s: Seq T, howMany: int): Seq T; let drop (#ty: Type) (s: seq ty) (howMany: nat{howMany <= length s}) : seq ty = let _, result = FLT.splitAt howMany s in result /// We represent the Dafny function `Seq#Equal` with `equal`. /// /// function Seq#Equal<T>(Seq T, Seq T): bool; let equal (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 == length s1 /\ (forall j.{:pattern index s0 j \/ index s1 j} 0 <= j && j < length s0 ==> index s0 j == index s1 j) /// Instead of representing the Dafny function `Seq#SameUntil`, which /// is only ever used in Dafny to represent prefix relations, we /// instead use `is_prefix`. /// /// function Seq#SameUntil<T>(Seq T, Seq T, int): bool; let is_prefix (#ty: Type) (s0: seq ty) (s1: seq ty) : Type0 = length s0 <= length s1 /\ (forall (j: nat).{:pattern index s0 j \/ index s1 j} j < length s0 ==> index s0 j == index s1 j) /// We represent the Dafny function `Seq#Rank` with `rank`. /// /// function Seq#Rank<T>(Seq T): int; let rank (#ty: Type) (v: ty) = v /// We now prove each of the facts that comprise `all_seq_facts`. /// For fact `xxx_fact`, we prove it with `xxx_lemma`. Sometimes, that /// requires a helper lemma, which we call `xxx_helper`. In some cases, /// we need multiple helpers, so we suffix their names with integers. private let length_of_empty_is_zero_lemma () : Lemma (length_of_empty_is_zero_fact) = () private let length_zero_implies_empty_lemma () : Lemma (length_zero_implies_empty_fact) = () private let singleton_length_one_lemma () : Lemma (singleton_length_one_fact) = () private let build_increments_length_lemma () : Lemma (build_increments_length_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty). length (build s v) = 1 + length s with ( FLT.append_length s [v] ) private let rec index_into_build_helper (#ty: Type) (s: list ty) (v: ty) (i: nat{i < length (append s [v])}) : Lemma (requires i <= length s) (ensures index (append s [v]) i == (if i = length s then v else index s i)) = FLT.append_length s [v]; match s with | [] -> () | hd :: tl -> if i = 0 then () else index_into_build_helper tl v (i - 1) private let index_into_build_lemma () : Lemma (requires build_increments_length_fact u#a) (ensures index_into_build_fact u#a ()) = introduce forall (ty: Type) (s: seq ty) (v: ty) (i: nat{i < length (build s v)}). (i = length s ==> index (build s v) i == v) /\ (i <> length s ==> index (build s v) i == index s i) with ( index_into_build_helper u#a s v i ) private let append_sums_lengths_lemma () : Lemma (append_sums_lengths_fact) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty). length (append s0 s1) = length s0 + length s1 with ( FLT.append_length s0 s1 ) private let index_into_singleton_lemma (_: squash (singleton_length_one_fact u#a)) : Lemma (index_into_singleton_fact u#a ()) = () private let rec index_after_append_helper (ty: Type) (s0: list ty) (s1: list ty) (n: nat) : Lemma (requires n < length (append s0 s1) && length (append s0 s1) = length s0 + length s1) (ensures index (append s0 s1) n == (if n < length s0 then index s0 n else index s1 (n - length s0))) = match s0 with | [] -> () | hd :: tl -> if n = 0 then () else index_after_append_helper ty tl s1 (n - 1) private let index_after_append_lemma (_: squash (append_sums_lengths_fact u#a)) : Lemma (index_after_append_fact u#a ()) = introduce forall (ty: Type) (s0: seq ty) (s1: seq ty) (n: nat{n < length (append s0 s1)}). (n < length s0 ==> index (append s0 s1) n == index s0 n) /\ (length s0 <= n ==> index (append s0 s1) n == index s1 (n - length s0)) with ( index_after_append_helper ty s0 s1 n ) private let rec lemma_splitAt_fst_length (#a:Type) (n:nat) (l:list a) : Lemma (requires (n <= length l)) (ensures (length (fst (FLT.splitAt n l)) = n)) = match n, l with | 0, _ -> () | _, [] -> () | _, _ :: l' -> lemma_splitAt_fst_length (n - 1) l' private let update_maintains_length_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) : Lemma (length (update s i v) = length s) = let s1, _, s2 = FLT.split3 s i in lemma_splitAt_fst_length i s; FLT.lemma_splitAt_snd_length i s; FLT.append_length [v] s2; FLT.append_length s1 (append [v] s2) private let update_maintains_length_lemma () : Lemma (update_maintains_length_fact) = introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty). length (update s i v) = length s with ( update_maintains_length_helper s i v ) private let rec update_then_index_helper (#ty: Type) (s: list ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}) : Lemma (requires n < length s) (ensures index (update s i v) n == (if i = n then v else index s n)) = match s with | hd :: tl -> if i = 0 || n = 0 then () else update_then_index_helper tl (i - 1) v (n - 1) private let update_then_index_lemma () : Lemma (update_then_index_fact) = update_maintains_length_lemma (); introduce forall (ty: Type) (s: seq ty) (i: nat{i < length s}) (v: ty) (n: nat{n < length (update s i v)}). n < length s ==> (i = n ==> index (update s i v) n == v) /\ (i <> n ==> index (update s i v) n == index s n) with introduce _ ==> _ with given_antecedent. ( update_then_index_helper s i v n ) private let contains_iff_exists_index_lemma () : Lemma (contains_iff_exists_index_fact) = introduce forall (ty: Type) (s: seq ty) (x: ty). contains s x <==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with ( introduce contains s x ==> (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) with given_antecedent. ( introduce exists (i: nat). i < length s /\ index s i == x with (FLT.index_of s x) and () ); introduce (exists (i: nat).{:pattern index s i} i < length s /\ index s i == x) ==> contains s x with given_antecedent. ( eliminate exists (i: nat). i < length s /\ index s i == x returns _ with _. FLT.lemma_index_memP s i ) ) private let empty_doesnt_contain_anything_lemma () : Lemma (empty_doesnt_contain_anything_fact) = () private let rec build_contains_equiv_helper (ty: Type) (s: list ty) (v: ty) (x: ty) : Lemma (FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s)) = match s with | [] -> () | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (append s [v]) <==> (v == x \/ FLT.memP x s) with _. () and _. build_contains_equiv_helper ty tl v x private let build_contains_equiv_lemma () : Lemma (build_contains_equiv_fact) = introduce forall (ty: Type) (s: seq ty) (v: ty) (x: ty). contains (build s v) x <==> (v == x \/ contains s x) with ( build_contains_equiv_helper ty s v x ) private let rec take_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (take s n)) (ensures (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with case_x_eq_hd. assert(index s 0 == x) and case_x_ne_hd. ( take_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). i_tl < n - 1 /\ i_tl < length tl /\ index tl i_tl == x returns exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x with _. introduce exists (i: nat). i < n /\ i < length s /\ index s i == x with (i_tl + 1) and ()) private let rec take_contains_equiv_exists_helper2 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) (i: nat) : Lemma (requires (i < n /\ i < length s /\ index s i == x)) (ensures FLT.memP x (take s n)) = match s with | hd :: tl -> eliminate x == hd \/ ~(x == hd) returns FLT.memP x (take s n) with case_x_eq_hd. () and case_x_ne_hd. take_contains_equiv_exists_helper2 ty tl (n - 1) x (i - 1) private let take_contains_equiv_exists_helper3 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (FLT.memP x (take s n) <==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x)) = introduce FLT.memP x (take s n) ==> (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) with given_antecedent. (take_contains_equiv_exists_helper1 ty s n x); introduce (exists (i: nat).{:pattern index s i} i < n /\ i < length s /\ index s i == x) ==> FLT.memP x (take s n) with given_antecedent. ( eliminate exists (i: nat). i < n /\ i < length s /\ index s i == x returns _ with _. take_contains_equiv_exists_helper2 ty s n x i ) private let take_contains_equiv_exists_lemma () : Lemma (take_contains_equiv_exists_fact) = introduce forall (ty: Type) (s: seq ty) (n: nat{n <= length s}) (x: ty). contains (take s n) x <==> (exists (i: nat). i < n /\ i < length s /\ index s i == x) with ( take_contains_equiv_exists_helper3 ty s n x )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.Properties.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Classical.Sugar.fsti.checked" ], "interface_file": true, "source_file": "FStar.Sequence.Base.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "FLT" }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Sequence", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": 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": 5, "z3rlimit_factor": 10, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ty: Type -> s: Prims.list ty -> n: Prims.nat{n <= FStar.Sequence.Base.length s} -> x: ty -> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.memP x (FStar.Sequence.Base.drop s n)) (ensures exists (i: Prims.nat). {:pattern FStar.Sequence.Base.index s i} n <= i /\ i < FStar.Sequence.Base.length s /\ FStar.Sequence.Base.index s i == x)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Prims.nat", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Sequence.Base.length", "FStar.Classical.Sugar.or_elim", "Prims.eq2", "Prims.int", "Prims.squash", "Prims.l_not", "Prims.op_disEquality", "Prims.l_Exists", "Prims.l_and", "Prims.op_LessThan", "FStar.Sequence.Base.index", "Prims.op_GreaterThanOrEqual", "Prims._assert", "FStar.Classical.Sugar.exists_elim", "FStar.Classical.Sugar.exists_intro", "Prims.op_Addition", "Prims.unit", "FStar.Sequence.Base.drop_contains_equiv_exists_helper1", "Prims.op_Subtraction", "FStar.List.Tot.Base.memP", "FStar.Sequence.Base.drop", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec drop_contains_equiv_exists_helper1 (ty: Type) (s: list ty) (n: nat{n <= length s}) (x: ty) : Lemma (requires FLT.memP x (drop s n)) (ensures (exists (i: nat). {:pattern index s i} n <= i /\ i < length s /\ index s i == x)) =
match s with | hd :: tl -> eliminate n == 0 \/ n <> 0 returns exists (i: nat). {:pattern index s i} n <= i /\ i < length s /\ index s i == x with case_n_eq_0 . (eliminate x == hd \/ ~(x == hd) returns _ with _ . assert (index s 0 == x) and _ . (drop_contains_equiv_exists_helper1 ty tl n x; eliminate exists (i_tl: nat). (n <= i_tl /\ i_tl < length tl /\ index tl i_tl == x) returns _ with _. introduce exists i.n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ())) and case_n_ne_0 . (drop_contains_equiv_exists_helper1 ty tl (n - 1) x; eliminate exists (i_tl: nat). n - 1 <= i_tl /\ i_tl < length tl /\ index tl i_tl == x returns _ with _. introduce exists i.n <= i /\ i < length s /\ index s i == x with (i_tl + 1) and ())
false
AlgWP.fst
AlgWP.noops
val noops:rwops
val noops:rwops
let noops : rwops = []
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 19, "start_col": 0, "start_line": 19 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]}
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
AlgWP.rwops
Prims.Tot
[ "total" ]
[]
[ "Prims.Nil", "Alg.op" ]
[]
false
false
false
true
false
let noops:rwops =
[]
false
AlgWP.fst
AlgWP.read_wp
val read_wp:st_wp state
val read_wp:st_wp state
let read_wp : st_wp state = fun s0 p -> p (s0, s0)
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 50, "end_line": 68, "start_col": 0, "start_line": 68 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
AlgWP.st_wp Alg.state
Prims.Tot
[ "total" ]
[]
[ "Alg.state", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2" ]
[]
false
false
false
true
false
let read_wp:st_wp state =
fun s0 p -> p (s0, s0)
false
AlgWP.fst
AlgWP.bind_wp
val bind_wp (#a #b: _) (w: st_wp a) (wf: (a -> st_wp b)) : st_wp b
val bind_wp (#a #b: _) (w: st_wp a) (wf: (a -> st_wp b)) : st_wp b
let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p)
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 65, "start_col": 0, "start_line": 63 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
w: AlgWP.st_wp a -> wf: (_: a -> AlgWP.st_wp b) -> AlgWP.st_wp b
Prims.Tot
[ "total" ]
[]
[ "AlgWP.st_wp", "Alg.state", "FStar.Pervasives.Native.tuple2" ]
[]
false
false
false
true
false
let bind_wp #a #b (w: st_wp a) (wf: (a -> st_wp b)) : st_wp b =
fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p)
false
AlgWP.fst
AlgWP.write_wp
val write_wp: state -> st_wp unit
val write_wp: state -> st_wp unit
let write_wp : state -> st_wp unit = fun s _ p -> p ((), s)
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 59, "end_line": 71, "start_col": 0, "start_line": 71 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: Alg.state -> AlgWP.st_wp Prims.unit
Prims.Tot
[ "total" ]
[]
[ "Alg.state", "FStar.Pervasives.Native.tuple2", "Prims.unit", "FStar.Pervasives.Native.Mktuple2" ]
[]
false
false
false
true
false
let write_wp: state -> st_wp unit =
fun s _ p -> p ((), s)
false
AlgWP.fst
AlgWP.interp_as_wp
val interp_as_wp (#a: _) (t: Alg.tree a [Read; Write]) : st_wp a
val interp_as_wp (#a: _) (t: Alg.tree a [Read; Write]) : st_wp a
let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 80, "start_col": 0, "start_line": 74 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: Alg.tree a [Alg.Read; Alg.Write] -> AlgWP.st_wp a
Prims.Tot
[ "total" ]
[]
[ "Alg.tree", "Prims.Cons", "Alg.op", "Alg.Read", "Alg.Write", "Prims.Nil", "AlgWP.return_wp", "Alg.op_inp", "Alg.op_out", "Alg.tree0", "AlgWP.bind_wp", "Alg.state", "AlgWP.read_wp", "AlgWP.interp_as_wp", "AlgWP.st_wp", "Prims.unit", "AlgWP.write_wp" ]
[ "recursion" ]
false
false
false
true
false
let rec interp_as_wp #a (t: Alg.tree a [Read; Write]) : st_wp a =
match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o: unit) -> interp_as_wp (k o))
false
Hacl.Impl.Ed25519.Ladder.fst
Hacl.Impl.Ed25519.Ladder.point_mul
val point_mul: out:point -> scalar:lbuffer uint8 32ul -> q:point -> Stack unit (requires fun h -> live h scalar /\ live h q /\ live h out /\ disjoint q out /\ disjoint q scalar /\ F51.point_inv_t h q /\ F51.inv_ext_point (as_seq h q)) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ F51.point_inv_t h1 out /\ F51.inv_ext_point (as_seq h1 out) /\ S.to_aff_point (F51.point_eval h1 out) == S.to_aff_point (S.point_mul (as_seq h0 scalar) (F51.point_eval h0 q)))
val point_mul: out:point -> scalar:lbuffer uint8 32ul -> q:point -> Stack unit (requires fun h -> live h scalar /\ live h q /\ live h out /\ disjoint q out /\ disjoint q scalar /\ F51.point_inv_t h q /\ F51.inv_ext_point (as_seq h q)) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ F51.point_inv_t h1 out /\ F51.inv_ext_point (as_seq h1 out) /\ S.to_aff_point (F51.point_eval h1 out) == S.to_aff_point (S.point_mul (as_seq h0 scalar) (F51.point_eval h0 q)))
let point_mul out scalar q = let h0 = ST.get () in SE.exp_fw_lemma S.mk_ed25519_concrete_ops (F51.point_eval h0 q) 256 (BSeq.nat_from_bytes_le (as_seq h0 scalar)) 4; push_frame (); let bscalar = create 4ul (u64 0) in convert_scalar scalar bscalar; point_mul_noalloc out bscalar q; pop_frame ()
{ "file_name": "code/ed25519/Hacl.Impl.Ed25519.Ladder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 96, "start_col": 0, "start_line": 88 }
module Hacl.Impl.Ed25519.Ladder module ST = FStar.HyperStack.ST open FStar.HyperStack.All open FStar.Mul open Lib.IntTypes open Lib.Buffer open Hacl.Bignum25519 module F51 = Hacl.Impl.Ed25519.Field51 module BSeq = Lib.ByteSequence module LE = Lib.Exponentiation module SE = Spec.Exponentiation module BE = Hacl.Impl.Exponentiation module ME = Hacl.Impl.MultiExponentiation module PT = Hacl.Impl.PrecompTable module SPT256 = Hacl.Spec.PrecompBaseTable256 module BD = Hacl.Bignum.Definitions module SD = Hacl.Spec.Bignum.Definitions module S = Spec.Ed25519 open Hacl.Impl.Ed25519.PointConstants include Hacl.Impl.Ed25519.Group include Hacl.Ed25519.PrecompTable #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" inline_for_extraction noextract let table_inv_w4 : BE.table_inv_t U64 20ul 16ul = [@inline_let] let len = 20ul in [@inline_let] let ctx_len = 0ul in [@inline_let] let k = mk_ed25519_concrete_ops in [@inline_let] let l = 4ul in [@inline_let] let table_len = 16ul in BE.table_inv_precomp len ctx_len k l table_len inline_for_extraction noextract let table_inv_w5 : BE.table_inv_t U64 20ul 32ul = [@inline_let] let len = 20ul in [@inline_let] let ctx_len = 0ul in [@inline_let] let k = mk_ed25519_concrete_ops in [@inline_let] let l = 5ul in [@inline_let] let table_len = 32ul in assert_norm (pow2 (v l) = v table_len); BE.table_inv_precomp len ctx_len k l table_len inline_for_extraction noextract val convert_scalar: scalar:lbuffer uint8 32ul -> bscalar:lbuffer uint64 4ul -> Stack unit (requires fun h -> live h scalar /\ live h bscalar /\ disjoint scalar bscalar) (ensures fun h0 _ h1 -> modifies (loc bscalar) h0 h1 /\ BD.bn_v h1 bscalar == BSeq.nat_from_bytes_le (as_seq h0 scalar)) let convert_scalar scalar bscalar = let h0 = ST.get () in Hacl.Spec.Bignum.Convert.bn_from_bytes_le_lemma #U64 32 (as_seq h0 scalar); Hacl.Bignum.Convert.mk_bn_from_bytes_le true 32ul scalar bscalar inline_for_extraction noextract val point_mul_noalloc: out:point -> bscalar:lbuffer uint64 4ul -> q:point -> Stack unit (requires fun h -> live h bscalar /\ live h q /\ live h out /\ disjoint q out /\ disjoint q bscalar /\ disjoint out bscalar /\ F51.point_inv_t h q /\ F51.inv_ext_point (as_seq h q) /\ BD.bn_v h bscalar < pow2 256) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ F51.point_inv_t h1 out /\ F51.inv_ext_point (as_seq h1 out) /\ S.to_aff_point (F51.point_eval h1 out) == LE.exp_fw S.mk_ed25519_comm_monoid (S.to_aff_point (F51.point_eval h0 q)) 256 (BD.bn_v h0 bscalar) 4) let point_mul_noalloc out bscalar q = BE.lexp_fw_consttime 20ul 0ul mk_ed25519_concrete_ops 4ul (null uint64) q 4ul 256ul bscalar out
{ "checked_file": "/", "dependencies": [ "Spec.Exponentiation.fsti.checked", "Spec.Ed25519.Lemmas.fsti.checked", "Spec.Ed25519.fst.checked", "prims.fst.checked", "LowStar.Ignore.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Exponentiation.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.PrecompBaseTable256.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "Hacl.Spec.Bignum.Convert.fst.checked", "Hacl.Impl.PrecompTable.fsti.checked", "Hacl.Impl.MultiExponentiation.fsti.checked", "Hacl.Impl.Exponentiation.fsti.checked", "Hacl.Impl.Ed25519.PointNegate.fst.checked", "Hacl.Impl.Ed25519.PointConstants.fst.checked", "Hacl.Impl.Ed25519.Group.fst.checked", "Hacl.Impl.Ed25519.Field51.fst.checked", "Hacl.Ed25519.PrecompTable.fsti.checked", "Hacl.Bignum25519.fsti.checked", "Hacl.Bignum.Definitions.fst.checked", "Hacl.Bignum.Convert.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked" ], "interface_file": true, "source_file": "Hacl.Impl.Ed25519.Ladder.fst" }
[ { "abbrev": false, "full_module": "Hacl.Ed25519.PrecompTable", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Ed25519.Group", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Ed25519.PointConstants", "short_module": null }, { "abbrev": true, "full_module": "Spec.Ed25519", "short_module": "S" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "SD" }, { "abbrev": true, "full_module": "Hacl.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Hacl.Spec.PrecompBaseTable256", "short_module": "SPT256" }, { "abbrev": true, "full_module": "Hacl.Impl.PrecompTable", "short_module": "PT" }, { "abbrev": true, "full_module": "Hacl.Impl.MultiExponentiation", "short_module": "ME" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": true, "full_module": "Lib.Exponentiation", "short_module": "LE" }, { "abbrev": true, "full_module": "Lib.ByteSequence", "short_module": "BSeq" }, { "abbrev": true, "full_module": "Hacl.Impl.Ed25519.Field51", "short_module": "F51" }, { "abbrev": false, "full_module": "Hacl.Bignum25519", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": true, "full_module": "Spec.Ed25519", "short_module": "S" }, { "abbrev": true, "full_module": "Hacl.Impl.Ed25519.Field51", "short_module": "F51" }, { "abbrev": true, "full_module": "Lib.ByteSequence", "short_module": "BSeq" }, { "abbrev": false, "full_module": "Hacl.Bignum25519", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "Hacl.Impl.Ed25519", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Ed25519", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 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.Bignum25519.point -> scalar: Lib.Buffer.lbuffer Lib.IntTypes.uint8 32ul -> q: Hacl.Bignum25519.point -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Bignum25519.point", "Lib.Buffer.lbuffer", "Lib.IntTypes.uint8", "FStar.UInt32.__uint_to_t", "FStar.HyperStack.ST.pop_frame", "Prims.unit", "Hacl.Impl.Ed25519.Ladder.point_mul_noalloc", "Hacl.Impl.Ed25519.Ladder.convert_scalar", "Lib.Buffer.lbuffer_t", "Lib.Buffer.MUT", "Lib.IntTypes.int_t", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "FStar.UInt32.uint_to_t", "FStar.UInt32.t", "Lib.Buffer.create", "Lib.IntTypes.uint64", "Lib.IntTypes.u64", "FStar.HyperStack.ST.push_frame", "Spec.Exponentiation.exp_fw_lemma", "Spec.Ed25519.ext_point_c", "Spec.Ed25519.mk_ed25519_concrete_ops", "Hacl.Impl.Ed25519.Field51.point_eval", "Lib.ByteSequence.nat_from_bytes_le", "Lib.Buffer.as_seq", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get" ]
[]
false
true
false
false
false
let point_mul out scalar q =
let h0 = ST.get () in SE.exp_fw_lemma S.mk_ed25519_concrete_ops (F51.point_eval h0 q) 256 (BSeq.nat_from_bytes_le (as_seq h0 scalar)) 4; push_frame (); let bscalar = create 4ul (u64 0) in convert_scalar scalar bscalar; point_mul_noalloc out bscalar q; pop_frame ()
false
AlgWP.fst
AlgWP.interp_as_wp2
val interp_as_wp2 (#a #l: _) (t: rwtree a l) : Alg (st_wp a) []
val interp_as_wp2 (#a #l: _) (t: rwtree a l) : Alg (st_wp a) []
let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k)))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 84, "end_line": 89, "start_col": 0, "start_line": 83 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: AlgWP.rwtree a l -> Alg.Alg (AlgWP.st_wp a)
Alg.Alg
[]
[]
[ "Alg.ops", "Alg.sublist", "Prims.Cons", "Alg.op", "Alg.Read", "Alg.Write", "Prims.Nil", "AlgWP.rwtree", "Alg.handle_with", "AlgWP.st_wp", "Prims.unit", "AlgWP.return_wp", "FStar.List.Tot.Base.memP", "Alg.op_inp", "Alg.op_out", "AlgWP.bind_wp", "Alg.state", "AlgWP.read_wp", "Alg.run", "AlgWP.write_wp", "Alg.handler_op", "Alg.tree" ]
[]
false
true
false
false
false
let interp_as_wp2 #a #l (t: rwtree a l) : Alg (st_wp a) [] =
let t0:Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function | Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k)))
false
AlgWP.fst
AlgWP.interp_ret'
val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x))
val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x))
let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 72, "end_line": 105, "start_col": 0, "start_line": 105 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: a -> FStar.Pervasives.Lemma (ensures AlgWP.return_wp x == AlgWP.interp_as_wp (Alg.Return x))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Pervasives.assert_norm", "Prims.eq2", "AlgWP.st_wp", "AlgWP.return_wp", "AlgWP.interp_as_wp", "Alg.Return", "Prims.unit" ]
[]
true
false
true
false
false
let interp_ret' x =
assert_norm (return_wp x == interp_as_wp (Return x))
false
AlgWP.fst
AlgWP.repr
val repr : a: Type -> l: AlgWP.rwops -> w: AlgWP.st_wp a -> Type
let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c}
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 90, "end_line": 183, "start_col": 0, "start_line": 183 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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 -> l: AlgWP.rwops -> w: AlgWP.st_wp a -> Type
Prims.Tot
[ "total" ]
[]
[ "AlgWP.rwops", "AlgWP.st_wp", "AlgWP.rwtree", "AlgWP.stronger", "AlgWP.interp_as_wp" ]
[]
false
false
false
true
true
let repr (a: Type) (l: rwops) (w: st_wp a) =
c: (rwtree a l){w `stronger` (interp_as_wp c)}
false
AlgWP.fst
AlgWP.bind
val bind (a b: Type) (#l1: rwops) (#wp_v: st_wp a) (#l2: rwops) (#wp_f: (a -> st_wp b)) (v: repr a l1 wp_v) (f: (x: a -> repr b l2 (wp_f x))) : repr b (l1 @@ l2) (bind_wp wp_v wp_f)
val bind (a b: Type) (#l1: rwops) (#wp_v: st_wp a) (#l2: rwops) (#wp_f: (a -> st_wp b)) (v: repr a l1 wp_v) (f: (x: a -> repr b l2 (wp_f x))) : repr b (l1 @@ l2) (bind_wp wp_v wp_f)
let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 13, "end_line": 195, "start_col": 0, "start_line": 189 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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 -> v: AlgWP.repr a l1 wp_v -> f: (x: a -> AlgWP.repr b l2 (wp_f x)) -> AlgWP.repr b (l1 @@ l2) (AlgWP.bind_wp wp_v wp_f)
Prims.Tot
[ "total" ]
[]
[ "AlgWP.rwops", "AlgWP.st_wp", "AlgWP.repr", "AlgWP.tbind", "Prims.unit", "AlgWP.interp_bind", "AlgWP.op_At_At", "AlgWP.bind_wp" ]
[]
false
false
false
false
false
let bind (a b: Type) (#l1: rwops) (#wp_v: st_wp a) (#l2: rwops) (#wp_f: (a -> st_wp b)) (v: repr a l1 wp_v) (f: (x: a -> repr b l2 (wp_f x))) : repr b (l1 @@ l2) (bind_wp wp_v wp_f) =
interp_bind v f wp_v wp_f; tbind v f
false
Pulse.Checker.Prover.ElimExists.fst
Pulse.Checker.Prover.ElimExists.elim_exists_frame
val elim_exists_frame (#g: env) (#ctxt #frame: vprop) (ctxt_frame_typing: tot_typing g (ctxt * frame) tm_vprop) (uvs: env{disjoint uvs g}) : T.Tac (g': env{env_extends g' g /\ disjoint uvs g'} & ctxt': term & tot_typing g' (ctxt' * frame) tm_vprop & continuation_elaborator g (ctxt * frame) g' (ctxt' * frame))
val elim_exists_frame (#g: env) (#ctxt #frame: vprop) (ctxt_frame_typing: tot_typing g (ctxt * frame) tm_vprop) (uvs: env{disjoint uvs g}) : T.Tac (g': env{env_extends g' g /\ disjoint uvs g'} & ctxt': term & tot_typing g' (ctxt' * frame) tm_vprop & continuation_elaborator g (ctxt * frame) g' (ctxt' * frame))
let elim_exists_frame (#g:env) (#ctxt #frame:vprop) (ctxt_frame_typing:tot_typing g (ctxt * frame) tm_vprop) (uvs:env { disjoint uvs g }) : T.Tac (g':env { env_extends g' g /\ disjoint uvs g' } & ctxt':term & tot_typing g' (ctxt' * frame) tm_vprop & continuation_elaborator g (ctxt * frame) g' (ctxt' * frame)) = add_elims should_elim_exists mk ctxt_frame_typing uvs
{ "file_name": "lib/steel/pulse/Pulse.Checker.Prover.ElimExists.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 55, "end_line": 58, "start_col": 0, "start_line": 51 }
(* 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.Checker.Prover.ElimExists open Pulse.Syntax open Pulse.Typing open Pulse.Typing.Combinators module T = FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open Pulse.Checker.VPropEquiv open Pulse.Checker.Prover.Base let should_elim_exists (v:vprop) : T.Tac bool = match v.t with | Tm_ExistsSL _ _ _ -> true | _ -> false let mk (#g:env) (#v:vprop) (v_typing:tot_typing g v tm_vprop) : T.Tac (option (x:ppname & t:st_term & c:comp { stateful_comp c /\ comp_pre c == v } & st_typing g t c)) = match v.t with | Tm_ExistsSL u { binder_ppname=nm; binder_ty = t } p -> let x = fresh g in let c = Pulse.Typing.comp_elim_exists u t p (nm, x) in let tm_typing : st_typing g _ c = T_ElimExists g (comp_u c) t p x (RU.magic()) (RU.magic()) in Some (| nm, _, c, tm_typing |) | _ -> None
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.Combinators.fsti.checked", "Pulse.Typing.fst.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.Checker.VPropEquiv.fsti.checked", "Pulse.Checker.Prover.Base.fsti.checked", "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Pulse.Checker.Prover.ElimExists.fst" }
[ { "abbrev": false, "full_module": "Pulse.Checker.Prover.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.VPropEquiv", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "Pulse.Typing.Combinators", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "Pulse.Checker.Prover.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Prover", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Prover", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
ctxt_frame_typing: Pulse.Typing.tot_typing g (ctxt * frame) Pulse.Syntax.Base.tm_vprop -> uvs: Pulse.Typing.Env.env{Pulse.Typing.Env.disjoint uvs g} -> FStar.Tactics.Effect.Tac (FStar.Pervasives.dtuple4 (g': Pulse.Typing.Env.env {Pulse.Typing.Env.env_extends g' g /\ Pulse.Typing.Env.disjoint uvs g'}) (fun _ -> Pulse.Syntax.Base.term) (fun g' ctxt' -> Pulse.Typing.tot_typing g' (ctxt' * frame) Pulse.Syntax.Base.tm_vprop) (fun g' ctxt' _ -> Pulse.Checker.Base.continuation_elaborator g (ctxt * frame) g' (ctxt' * frame)))
FStar.Tactics.Effect.Tac
[]
[]
[ "Pulse.Typing.Env.env", "Pulse.Syntax.Base.vprop", "Pulse.Typing.tot_typing", "Pulse.Checker.Prover.Base.op_Star", "Pulse.Syntax.Base.tm_vprop", "Pulse.Typing.Env.disjoint", "Pulse.Checker.Prover.Base.add_elims", "Pulse.Checker.Prover.ElimExists.should_elim_exists", "Pulse.Checker.Prover.ElimExists.mk", "FStar.Pervasives.dtuple4", "Prims.l_and", "Pulse.Typing.Env.env_extends", "Pulse.Syntax.Base.term", "Pulse.Syntax.Base.tm_star", "Pulse.Checker.Base.continuation_elaborator" ]
[]
false
true
false
false
false
let elim_exists_frame (#g: env) (#ctxt #frame: vprop) (ctxt_frame_typing: tot_typing g (ctxt * frame) tm_vprop) (uvs: env{disjoint uvs g}) : T.Tac (g': env{env_extends g' g /\ disjoint uvs g'} & ctxt': term & tot_typing g' (ctxt' * frame) tm_vprop & continuation_elaborator g (ctxt * frame) g' (ctxt' * frame)) =
add_elims should_elim_exists mk ctxt_frame_typing uvs
false
AlgWP.fst
AlgWP.subcomp
val subcomp (a: Type) (#l1: rwops) (#w1: st_wp a) (#l2: rwops) (#w2: st_wp a) (f: repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True)
val subcomp (a: Type) (#l1: rwops) (#w1: st_wp a) (#l2: rwops) (#w2: st_wp a) (f: repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True)
let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 202, "start_col": 0, "start_line": 197 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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 -> f: AlgWP.repr a l1 w1 -> Prims.Pure (AlgWP.repr a l2 w2)
Prims.Pure
[]
[]
[ "AlgWP.rwops", "AlgWP.st_wp", "AlgWP.repr", "Prims.l_and", "AlgWP.stronger", "AlgWP.subops", "Prims.l_True" ]
[]
false
false
false
false
false
let subcomp (a: Type) (#l1: rwops) (#w1: st_wp a) (#l2: rwops) (#w2: st_wp a) (f: repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) =
f
false
AlgWP.fst
AlgWP.is_mono
val is_mono (#a: _) (w: st_wp a) : Type0
val is_mono (#a: _) (w: st_wp a) : Type0
let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 105, "end_line": 274, "start_col": 0, "start_line": 274 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
w: AlgWP.st_wp a -> Type0
Prims.Tot
[ "total" ]
[]
[ "AlgWP.st_wp", "Prims.l_Forall", "Alg.state", "FStar.Pervasives.Native.tuple2", "Prims.logical", "Prims.l_imp" ]
[]
false
false
false
true
true
let is_mono #a (w: st_wp a) : Type0 =
forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2
false
AlgWP.fst
AlgWP.interp_monotonic
val interp_monotonic (#a #l: _) (c: rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c))
val interp_monotonic (#a #l: _) (c: rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c))
let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 65, "end_line": 129, "start_col": 0, "start_line": 115 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: AlgWP.rwtree a l -> FStar.Pervasives.Lemma (ensures AlgWP.wp_is_monotonic (AlgWP.interp_as_wp c))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Alg.ops", "Alg.sublist", "Prims.Cons", "Alg.op", "Alg.Read", "Alg.Write", "Prims.Nil", "AlgWP.rwtree", "Alg.op_inp", "Alg.op_out", "Alg.tree0", "AlgWP.bind_preserves_mon", "Alg.state", "AlgWP.read_wp", "AlgWP.interp_as_wp", "AlgWP.st_wp", "Prims.unit", "FStar.Classical.forall_intro", "AlgWP.wp_is_monotonic", "Prims.l_True", "Prims.squash", "FStar.Pervasives.pattern", "AlgWP.interp_monotonic", "AlgWP.write_wp" ]
[ "recursion" ]
false
false
true
false
false
let rec interp_monotonic #a #l (c: rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) =
match c with | Return x -> () | Op Read _ k -> let aux (x: state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x: unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x))
false
AlgWP.fst
AlgWP.is_ro
val is_ro (#a: _) (w: st_wp a) : Type0
val is_ro (#a: _) (w: st_wp a) : Type0
let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 28, "end_line": 277, "start_col": 0, "start_line": 276 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
w: AlgWP.st_wp a -> Type0
Prims.Tot
[ "total" ]
[]
[ "AlgWP.st_wp", "AlgWP.stronger", "AlgWP.quotient_ro" ]
[]
false
false
false
true
true
let is_ro #a (w: st_wp a) : Type0 =
(quotient_ro w) `stronger` w
false
AlgWP.fst
AlgWP.lift_pure_wp
val lift_pure_wp (#a: Type) (wp: pure_wp a) : st_wp a
val lift_pure_wp (#a: Type) (wp: pure_wp a) : st_wp a
let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 37, "end_line": 230, "start_col": 0, "start_line": 228 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
wp: Prims.pure_wp a -> AlgWP.st_wp a
Prims.Tot
[ "total" ]
[]
[ "Prims.pure_wp", "Alg.state", "FStar.Pervasives.Native.tuple2", "Prims.l_True", "FStar.Pervasives.Native.Mktuple2", "Prims.pure_pre", "Prims.unit", "FStar.Monotonic.Pure.elim_pure_wp_monotonicity", "AlgWP.st_wp" ]
[]
false
false
false
true
false
let lift_pure_wp (#a: Type) (wp: pure_wp a) : st_wp a =
FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0))
false
Pulse.Checker.Prover.ElimExists.fst
Pulse.Checker.Prover.ElimExists.elim_exists
val elim_exists (#g:env) (#ctxt:term) (ctxt_typing:tot_typing g ctxt tm_vprop) : T.Tac (g':env { env_extends g' g } & ctxt':term & tot_typing g' ctxt' tm_vprop & continuation_elaborator g ctxt g' ctxt')
val elim_exists (#g:env) (#ctxt:term) (ctxt_typing:tot_typing g ctxt tm_vprop) : T.Tac (g':env { env_extends g' g } & ctxt':term & tot_typing g' ctxt' tm_vprop & continuation_elaborator g ctxt g' ctxt')
let elim_exists (#g:env) (#ctxt:term) (ctxt_typing:tot_typing g ctxt tm_vprop) : T.Tac (g':env { env_extends g' g } & ctxt':term & tot_typing g' ctxt' tm_vprop & continuation_elaborator g ctxt g' ctxt') = let ctxt_emp_typing : tot_typing g (tm_star ctxt tm_emp) tm_vprop = RU.magic () in let (| g', ctxt', ctxt'_emp_typing, k |) = elim_exists_frame ctxt_emp_typing (mk_env (fstar_env g)) in let k = k_elab_equiv k (VE_Trans _ _ _ _ (VE_Comm _ _ _) (VE_Unit _ _)) (VE_Trans _ _ _ _ (VE_Comm _ _ _) (VE_Unit _ _)) in (| g', ctxt', star_typing_inversion_l ctxt'_emp_typing, k |)
{ "file_name": "lib/steel/pulse/Pulse.Checker.Prover.ElimExists.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 62, "end_line": 72, "start_col": 0, "start_line": 60 }
(* 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.Checker.Prover.ElimExists open Pulse.Syntax open Pulse.Typing open Pulse.Typing.Combinators module T = FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open Pulse.Checker.VPropEquiv open Pulse.Checker.Prover.Base let should_elim_exists (v:vprop) : T.Tac bool = match v.t with | Tm_ExistsSL _ _ _ -> true | _ -> false let mk (#g:env) (#v:vprop) (v_typing:tot_typing g v tm_vprop) : T.Tac (option (x:ppname & t:st_term & c:comp { stateful_comp c /\ comp_pre c == v } & st_typing g t c)) = match v.t with | Tm_ExistsSL u { binder_ppname=nm; binder_ty = t } p -> let x = fresh g in let c = Pulse.Typing.comp_elim_exists u t p (nm, x) in let tm_typing : st_typing g _ c = T_ElimExists g (comp_u c) t p x (RU.magic()) (RU.magic()) in Some (| nm, _, c, tm_typing |) | _ -> None let elim_exists_frame (#g:env) (#ctxt #frame:vprop) (ctxt_frame_typing:tot_typing g (ctxt * frame) tm_vprop) (uvs:env { disjoint uvs g }) : T.Tac (g':env { env_extends g' g /\ disjoint uvs g' } & ctxt':term & tot_typing g' (ctxt' * frame) tm_vprop & continuation_elaborator g (ctxt * frame) g' (ctxt' * frame)) = add_elims should_elim_exists mk ctxt_frame_typing uvs
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.Combinators.fsti.checked", "Pulse.Typing.fst.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.Checker.VPropEquiv.fsti.checked", "Pulse.Checker.Prover.Base.fsti.checked", "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Pulse.Checker.Prover.ElimExists.fst" }
[ { "abbrev": false, "full_module": "Pulse.Checker.Prover.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.VPropEquiv", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "Pulse.Typing.Combinators", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "Pulse.Checker.Prover.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Prover", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Prover", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
ctxt_typing: Pulse.Typing.tot_typing g ctxt Pulse.Syntax.Base.tm_vprop -> FStar.Tactics.Effect.Tac (FStar.Pervasives.dtuple4 (g': Pulse.Typing.Env.env{Pulse.Typing.Env.env_extends g' g}) (fun _ -> Pulse.Syntax.Base.term) (fun g' ctxt' -> Pulse.Typing.tot_typing g' ctxt' Pulse.Syntax.Base.tm_vprop) (fun g' ctxt' _ -> Pulse.Checker.Base.continuation_elaborator g ctxt g' ctxt'))
FStar.Tactics.Effect.Tac
[]
[]
[ "Pulse.Typing.Env.env", "Pulse.Syntax.Base.term", "Pulse.Typing.tot_typing", "Pulse.Syntax.Base.tm_vprop", "Prims.l_and", "Pulse.Typing.Env.env_extends", "Pulse.Typing.Env.disjoint", "Pulse.Typing.Env.mk_env", "Pulse.Typing.Env.fstar_env", "Pulse.Checker.Prover.Base.op_Star", "Pulse.Syntax.Base.tm_emp", "Pulse.Checker.Base.continuation_elaborator", "FStar.Pervasives.Mkdtuple4", "Pulse.Typing.star_typing_inversion_l", "Pulse.Checker.Base.k_elab_equiv", "Pulse.Typing.VE_Trans", "Pulse.Syntax.Base.tm_star", "Pulse.Typing.VE_Comm", "Pulse.Typing.VE_Unit", "FStar.Pervasives.dtuple4", "Pulse.Checker.Prover.ElimExists.elim_exists_frame", "Pulse.RuntimeUtils.magic" ]
[]
false
true
false
false
false
let elim_exists (#g: env) (#ctxt: term) (ctxt_typing: tot_typing g ctxt tm_vprop) : T.Tac (g': env{env_extends g' g} & ctxt': term & tot_typing g' ctxt' tm_vprop & continuation_elaborator g ctxt g' ctxt') =
let ctxt_emp_typing:tot_typing g (tm_star ctxt tm_emp) tm_vprop = RU.magic () in let (| g' , ctxt' , ctxt'_emp_typing , k |) = elim_exists_frame ctxt_emp_typing (mk_env (fstar_env g)) in let k = k_elab_equiv k (VE_Trans _ _ _ _ (VE_Comm _ _ _) (VE_Unit _ _)) (VE_Trans _ _ _ _ (VE_Comm _ _ _) (VE_Unit _ _)) in (| g', ctxt', star_typing_inversion_l ctxt'_emp_typing, k |)
false
AlgWP.fst
AlgWP.get
val get: Prims.unit -> AlgWP state [Read] read_wp
val get: Prims.unit -> AlgWP state [Read] read_wp
let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return)
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 36, "end_line": 222, "start_col": 0, "start_line": 221 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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 -> AlgWP.AlgWP Alg.state
AlgWP.AlgWP
[]
[]
[ "Prims.unit", "Alg.Op", "Alg.state", "Alg.Read", "Alg.Return", "Prims.Cons", "Alg.op", "Prims.Nil", "AlgWP.read_wp" ]
[]
false
true
false
false
false
let get () : AlgWP state [Read] read_wp =
AlgWP?.reflect (Op Read () Return)
false
AlgWP.fst
AlgWP.return
val return (a: Type) (x: a) : repr a noops (return_wp x)
val return (a: Type) (x: a) : repr a noops (return_wp x)
let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 10, "end_line": 187, "start_col": 0, "start_line": 185 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c}
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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 -> AlgWP.repr a AlgWP.noops (AlgWP.return_wp x)
Prims.Tot
[ "total" ]
[]
[ "Alg.Return", "Prims.unit", "AlgWP.interp_ret", "Prims.Nil", "Alg.op", "AlgWP.repr", "AlgWP.noops", "AlgWP.return_wp" ]
[]
false
false
false
false
false
let return (a: Type) (x: a) : repr a noops (return_wp x) =
interp_ret #_ #[] x; Return x
false
AlgWP.fst
AlgWP.interp_bind
val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f))
val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f))
let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 30, "end_line": 181, "start_col": 0, "start_line": 167 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: AlgWP.rwtree a l1 -> f: (_: a -> AlgWP.rwtree b l2) -> w1: AlgWP.st_wp a -> w2: (_: a -> AlgWP.st_wp b) -> FStar.Pervasives.Lemma (requires w1 <<= AlgWP.interp_as_wp c /\ (forall (x: a). w2 x <<= AlgWP.interp_as_wp (f x))) (ensures AlgWP.stronger (AlgWP.bind_wp w1 w2) (AlgWP.interp_as_wp (AlgWP.tbind c f)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "AlgWP.rwops", "AlgWP.rwtree", "AlgWP.st_wp", "FStar.Classical.forall_intro_2", "FStar.Pervasives.Native.tuple2", "Alg.state", "Prims.l_imp", "AlgWP.bind_wp", "AlgWP.interp_as_wp", "AlgWP.tbind", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "FStar.Calc.calc_finish", "Prims.logical", "Prims.Cons", "FStar.Preorder.relation", "FStar.Calc.calc_step", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Calc.calc_push_impl", "AlgWP.interp_monotonic", "AlgWP.interp_morph" ]
[]
false
false
true
false
false
let interp_bind #a #b c f w1 w2 =
let aux (p: ((b & state) -> Type0)) (s0: state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc ( ==> ) { bind_wp w1 w2 s0 p; ( ==> ) { () } w1 s0 (fun (y, s1) -> w2 y s1 p); ( ==> ) { () } interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ( ==> ) { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ( ==> ) { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux
false
Pulse.Recursion.fst
Pulse.Recursion.freshen_binders
val freshen_binders (bs: binders) : Tot binders (decreases length bs)
val freshen_binders (bs: binders) : Tot binders (decreases length bs)
let rec freshen_binders (bs:binders) : Tot binders (decreases length bs) = match bs with | [] -> [] | b::bs -> let b' = freshen_binder b in let bs = map (subst_binder_typ [Stubs.Syntax.Syntax.NT (binder_to_namedv b |> FStar.Stubs.Reflection.V2.Builtins.pack_namedv) (binder_to_term b')]) bs in b' :: freshen_binders bs
{ "file_name": "lib/steel/pulse/Pulse.Recursion.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 28, "end_line": 70, "start_col": 0, "start_line": 63 }
(* 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.Recursion module R = FStar.Reflection.V2 module RT = FStar.Reflection.Typing module T = FStar.Tactics.V2 open FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open FStar.List.Tot open Pulse.Syntax open Pulse.Typing module P = Pulse.Syntax.Printer exception Splitlast_empty let rec splitlast #a (l : list a) : Tac (list a & a) = match l with | [] -> raise Splitlast_empty | [x] -> [], x | x::xs -> let init, last = splitlast xs in x::init, last exception Map2_length_mismatch let rec map2 #a #b #c (f : a -> b -> Tac c) (xs : list a) (ys : list b) : Tac (list c) = match xs, ys with | [], [] -> [] | x::xx, y::yy -> f x y :: map2 f xx yy | _ -> raise Map2_length_mismatch let debug_main g (s: unit -> Tac string) : Tac unit = if RU.debug_at_level (fstar_env g) "pulse.main" then print (s ()) else () let string_as_term (s:string) : R.term = R.pack_ln (R.Tv_Const (C_String s)) let freshen_binder (b:T.binder) : T.binder = { b with uniq = 10000 + b.uniq ; ppname = map_seal b.ppname (fun s -> s ^ "'") } let subst_binder_typ (s : FStar.Stubs.Syntax.Syntax.subst_t) (b : Tactics.NamedView.binder) : Tactics.NamedView.binder = { b with sort = FStar.Stubs.Reflection.V2.Builtins.subst_term s b.sort }
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.PP.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.SyntaxHelpers.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.NamedView.fsti.checked", "FStar.Stubs.Syntax.Syntax.fsti.checked", "FStar.Stubs.Reflection.V2.Builtins.fsti.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Reflection.Typing.fsti.checked", "FStar.Printf.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked" ], "interface_file": true, "source_file": "Pulse.Recursion.fst" }
[ { "abbrev": true, "full_module": "Pulse.Syntax.Printer", "short_module": "P" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "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
bs: FStar.Tactics.NamedView.binders -> Prims.Tot FStar.Tactics.NamedView.binders
Prims.Tot
[ "total", "" ]
[]
[ "FStar.Tactics.NamedView.binders", "Prims.Nil", "FStar.Tactics.NamedView.binder", "Prims.list", "Prims.Cons", "Pulse.Recursion.freshen_binders", "FStar.List.Tot.Base.map", "Pulse.Recursion.subst_binder_typ", "FStar.Stubs.Syntax.Syntax.subst_elt", "FStar.Stubs.Syntax.Syntax.NT", "FStar.Stubs.Reflection.V2.Builtins.pack_namedv", "FStar.Tactics.V2.SyntaxCoercions.binder_to_namedv", "FStar.Tactics.V2.SyntaxCoercions.binder_to_term", "Pulse.Recursion.freshen_binder" ]
[ "recursion" ]
false
false
false
true
false
let rec freshen_binders (bs: binders) : Tot binders (decreases length bs) =
match bs with | [] -> [] | b :: bs -> let b' = freshen_binder b in let bs = map (subst_binder_typ [ Stubs.Syntax.Syntax.NT (binder_to_namedv b |> FStar.Stubs.Reflection.V2.Builtins.pack_namedv) (binder_to_term b') ]) bs in b' :: freshen_binders bs
false
Pulse.Recursion.fst
Pulse.Recursion.splitlast
val splitlast (#a: _) (l: list a) : Tac (list a & a)
val splitlast (#a: _) (l: list a) : Tac (list a & a)
let rec splitlast #a (l : list a) : Tac (list a & a) = match l with | [] -> raise Splitlast_empty | [x] -> [], x | x::xs -> let init, last = splitlast xs in x::init, last
{ "file_name": "lib/steel/pulse/Pulse.Recursion.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 17, "end_line": 38, "start_col": 0, "start_line": 32 }
(* 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.Recursion module R = FStar.Reflection.V2 module RT = FStar.Reflection.Typing module T = FStar.Tactics.V2 open FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open FStar.List.Tot open Pulse.Syntax open Pulse.Typing module P = Pulse.Syntax.Printer exception Splitlast_empty
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.PP.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.SyntaxHelpers.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.NamedView.fsti.checked", "FStar.Stubs.Syntax.Syntax.fsti.checked", "FStar.Stubs.Reflection.V2.Builtins.fsti.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Reflection.Typing.fsti.checked", "FStar.Printf.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked" ], "interface_file": true, "source_file": "Pulse.Recursion.fst" }
[ { "abbrev": true, "full_module": "Pulse.Syntax.Printer", "short_module": "P" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "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
l: Prims.list a -> FStar.Tactics.Effect.Tac (Prims.list a * a)
FStar.Tactics.Effect.Tac
[]
[]
[ "Prims.list", "FStar.Tactics.Effect.raise", "FStar.Pervasives.Native.tuple2", "Pulse.Recursion.Splitlast_empty", "FStar.Pervasives.Native.Mktuple2", "Prims.Nil", "Prims.Cons", "Pulse.Recursion.splitlast" ]
[ "recursion" ]
false
true
false
false
false
let rec splitlast #a (l: list a) : Tac (list a & a) =
match l with | [] -> raise Splitlast_empty | [x] -> [], x | x :: xs -> let init, last = splitlast xs in x :: init, last
false
Pulse.Recursion.fst
Pulse.Recursion.elab_b
val elab_b (qbv: option qualifier & binder & bv) : Tot Tactics.NamedView.binder
val elab_b (qbv: option qualifier & binder & bv) : Tot Tactics.NamedView.binder
let elab_b (qbv : option qualifier & binder & bv) : Tot Tactics.NamedView.binder = let q, b, bv = qbv in { uniq = bv.bv_index; ppname = b.binder_ppname.name; sort = elab_term b.binder_ty; qual = elab_qual q; attrs = []; }
{ "file_name": "lib/steel/pulse/Pulse.Recursion.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 3, "end_line": 80, "start_col": 0, "start_line": 72 }
(* 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.Recursion module R = FStar.Reflection.V2 module RT = FStar.Reflection.Typing module T = FStar.Tactics.V2 open FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open FStar.List.Tot open Pulse.Syntax open Pulse.Typing module P = Pulse.Syntax.Printer exception Splitlast_empty let rec splitlast #a (l : list a) : Tac (list a & a) = match l with | [] -> raise Splitlast_empty | [x] -> [], x | x::xs -> let init, last = splitlast xs in x::init, last exception Map2_length_mismatch let rec map2 #a #b #c (f : a -> b -> Tac c) (xs : list a) (ys : list b) : Tac (list c) = match xs, ys with | [], [] -> [] | x::xx, y::yy -> f x y :: map2 f xx yy | _ -> raise Map2_length_mismatch let debug_main g (s: unit -> Tac string) : Tac unit = if RU.debug_at_level (fstar_env g) "pulse.main" then print (s ()) else () let string_as_term (s:string) : R.term = R.pack_ln (R.Tv_Const (C_String s)) let freshen_binder (b:T.binder) : T.binder = { b with uniq = 10000 + b.uniq ; ppname = map_seal b.ppname (fun s -> s ^ "'") } let subst_binder_typ (s : FStar.Stubs.Syntax.Syntax.subst_t) (b : Tactics.NamedView.binder) : Tactics.NamedView.binder = { b with sort = FStar.Stubs.Reflection.V2.Builtins.subst_term s b.sort } let rec freshen_binders (bs:binders) : Tot binders (decreases length bs) = match bs with | [] -> [] | b::bs -> let b' = freshen_binder b in let bs = map (subst_binder_typ [Stubs.Syntax.Syntax.NT (binder_to_namedv b |> FStar.Stubs.Reflection.V2.Builtins.pack_namedv) (binder_to_term b')]) bs in b' :: freshen_binders bs
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.PP.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.SyntaxHelpers.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.NamedView.fsti.checked", "FStar.Stubs.Syntax.Syntax.fsti.checked", "FStar.Stubs.Reflection.V2.Builtins.fsti.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Reflection.Typing.fsti.checked", "FStar.Printf.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked" ], "interface_file": true, "source_file": "Pulse.Recursion.fst" }
[ { "abbrev": true, "full_module": "Pulse.Syntax.Printer", "short_module": "P" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "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
qbv: ((FStar.Pervasives.Native.option Pulse.Syntax.Base.qualifier * Pulse.Syntax.Base.binder) * Pulse.Syntax.Base.bv) -> FStar.Tactics.NamedView.binder
Prims.Tot
[ "total" ]
[]
[ "FStar.Pervasives.Native.tuple3", "FStar.Pervasives.Native.option", "Pulse.Syntax.Base.qualifier", "Pulse.Syntax.Base.binder", "Pulse.Syntax.Base.bv", "FStar.Tactics.NamedView.Mkbinder", "Pulse.Syntax.Base.__proj__Mkbv__item__bv_index", "Pulse.Syntax.Base.__proj__Mkppname__item__name", "Pulse.Syntax.Base.__proj__Mkbinder__item__binder_ppname", "Pulse.Elaborate.Pure.elab_term", "Pulse.Syntax.Base.__proj__Mkbinder__item__binder_ty", "Pulse.Elaborate.Pure.elab_qual", "Prims.Nil", "FStar.Tactics.NamedView.term", "FStar.Tactics.NamedView.binder" ]
[]
false
false
false
true
false
let elab_b (qbv: option qualifier & binder & bv) : Tot Tactics.NamedView.binder =
let q, b, bv = qbv in { uniq = bv.bv_index; ppname = b.binder_ppname.name; sort = elab_term b.binder_ty; qual = elab_qual q; attrs = [] }
false
Pulse.Recursion.fst
Pulse.Recursion.tie_knot
val tie_knot (g : env) (rng : R.range) (nm_orig : string) (nm_aux : string) (d : decl) (r_typ : R.term) (blob:RT.blob) : Tac (list (RT.sigelt_for (fstar_env g)))
val tie_knot (g : env) (rng : R.range) (nm_orig : string) (nm_aux : string) (d : decl) (r_typ : R.term) (blob:RT.blob) : Tac (list (RT.sigelt_for (fstar_env g)))
let tie_knot (g : env) (rng : R.range) (nm_orig nm_aux : string) (d : decl) (r_typ : R.typ) (blob:RT.blob) : Tac (list (RT.sigelt_for (fstar_env g))) = let knot_r_typ = (* Remove the last arguments from r_typ, as that is the recursive knot. After doing that, we now have the needed type for elaboration. *) let bs, c = collect_arr_bs r_typ in if Nil? bs then fail g (Some rng) "tie_knot: impossible (1)"; let bs = init bs in if Nil? bs then fail g (Some rng) "tie_knot: impossible (2)"; mk_arr bs c in (* This is a temporary implementation. It will just create a new letbinding at the appropriate type with a `RU.magic()` body. *) let flag, sig, _ = RT.mk_unchecked_let (fstar_env g) nm_orig (`(magic())) knot_r_typ in let nm = string_as_term nm_aux in let sig = RU.add_attribute sig (`("pulse.recursive.knot", `#(nm))) in [flag,sig,Some blob]
{ "file_name": "lib/steel/pulse/Pulse.Recursion.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 22, "end_line": 226, "start_col": 0, "start_line": 207 }
(* 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.Recursion module R = FStar.Reflection.V2 module RT = FStar.Reflection.Typing module T = FStar.Tactics.V2 open FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open FStar.List.Tot open Pulse.Syntax open Pulse.Typing module P = Pulse.Syntax.Printer exception Splitlast_empty let rec splitlast #a (l : list a) : Tac (list a & a) = match l with | [] -> raise Splitlast_empty | [x] -> [], x | x::xs -> let init, last = splitlast xs in x::init, last exception Map2_length_mismatch let rec map2 #a #b #c (f : a -> b -> Tac c) (xs : list a) (ys : list b) : Tac (list c) = match xs, ys with | [], [] -> [] | x::xx, y::yy -> f x y :: map2 f xx yy | _ -> raise Map2_length_mismatch let debug_main g (s: unit -> Tac string) : Tac unit = if RU.debug_at_level (fstar_env g) "pulse.main" then print (s ()) else () let string_as_term (s:string) : R.term = R.pack_ln (R.Tv_Const (C_String s)) let freshen_binder (b:T.binder) : T.binder = { b with uniq = 10000 + b.uniq ; ppname = map_seal b.ppname (fun s -> s ^ "'") } let subst_binder_typ (s : FStar.Stubs.Syntax.Syntax.subst_t) (b : Tactics.NamedView.binder) : Tactics.NamedView.binder = { b with sort = FStar.Stubs.Reflection.V2.Builtins.subst_term s b.sort } let rec freshen_binders (bs:binders) : Tot binders (decreases length bs) = match bs with | [] -> [] | b::bs -> let b' = freshen_binder b in let bs = map (subst_binder_typ [Stubs.Syntax.Syntax.NT (binder_to_namedv b |> FStar.Stubs.Reflection.V2.Builtins.pack_namedv) (binder_to_term b')]) bs in b' :: freshen_binders bs let elab_b (qbv : option qualifier & binder & bv) : Tot Tactics.NamedView.binder = let q, b, bv = qbv in { uniq = bv.bv_index; ppname = b.binder_ppname.name; sort = elab_term b.binder_ty; qual = elab_qual q; attrs = []; } let add_knot (g : env) (rng : R.range) (d : decl{FnDecl? d.d}) : Tac decl = let FnDecl { id; isrec; bs; comp; meas; body } = d.d in if Nil? bs then fail g (Some d.range) "main: FnDecl does not have binders"; (* NB: bs and comp are open *) let r_res = elab_comp comp in debug_main g (fun _ -> Printf.sprintf "add_knot: bs = %s\n" (string_of_list (fun (_, b,_) -> P.binder_to_string b) bs)); (* for fn rec f (x1:t1) ... (xn:tn) : requires pre returns x:a ensures post we elab into let f (x1:t1) ... (xn:tn) (f : (x1':t1) -> ... -> (xn':tn) -> stt a pre post) : stt a pre post without any sort of termination check. Now, for ghost fn rec f (x1:t1) ... (xn:tn) : requires pre returns x:a ensures post measure meas we must elab into let f (x1:t1) ... (xn:tn) (f : (x1':t1) -> ... -> (xn':tn){meas' << meas} -> stt a pre post) : stt a pre post so we need to add the measure refinement. Since `meas` is an open term (wrt x1...xn), we must substitute it to create meas', subtituting x1' for x1, ..., xn' for xn *) (* Desugaring added a recursive knot argument at the end *) let bs, b_knot = splitlast bs in (* freshen *) let r_bs0 = List.Tot.map elab_b bs in let r_bs = freshen_binders r_bs0 in let binder_to_r_namedv (b:T.binder) : R.namedv = R.pack_namedv { uniq = b.uniq; sort = seal b.sort; ppname = b.ppname; } in let prime_subst = map2 (fun (b1 b2 : T.binder) -> R.NT (binder_to_r_namedv b1) (binder_to_term b2)) r_bs0 r_bs in let r_bs = (* If ghost/atomic, we need to add a decreases refinement on the last arg *) if C_STAtomic? comp || C_STGhost? comp then ( if None? meas then ( let open FStar.Stubs.Pprint in let open Pulse.PP in fail_doc g (Some d.range) [ text "'ghost' and 'atomic' recursive functions require a 'decreases' clause"] ); let init, last = splitlast r_bs in let last : FStar.Tactics.NamedView.binder = last in let last = (* add a refinement to last *) let b' : simple_binder = { uniq = last.uniq; ppname = last.ppname; sort = last.sort; qual = Q_Explicit; attrs = []; } in let meas = Some?.v meas in let meas = elab_term meas in let meas' = R.subst_term prime_subst meas in let ref = `(`#meas' << `#meas) in (* TODO: this is not always printed *) let ref = (`labeled range_0 "Could not prove termination" (`#ref)) in { last with sort = (pack (Tv_Refine b' ref)) } in init @ [last] ) else r_bs in let r_res = R.subst_term prime_subst r_res in let r_ty = FStar.Tactics.V2.SyntaxHelpers.mk_tot_arr r_bs r_res in // let open FStar.Stubs.Pprint in // let open Pulse.PP in // warn_doc g (Some d.range) [ // text "r_ty (type of the knot binder) =" ^/^ pp r_ty // ]; if R.Tv_Unknown? (inspect_ln r_ty) then fail g (Some d.range) "error: r_ty is Tv_unknown in add_knot?"; let b_knot = let s, rng = inspect_ident id in let b = mk_binder s rng (tm_fstar r_ty rng) in let bv = { bv_index = b_knot._3.bv_index; bv_ppname = { name = seal s; range = rng } } in let q = None in (q, b, bv) in let id' = let s, rng = inspect_ident id in pack_ident ("__recaux_" ^ s, rng) in let bs' = bs @ [b_knot] in (* NB: body and comp unchanged, they are already shifted properly (we dropped one binder and added one) *) { d with d = FnDecl { id=id'; isrec=false; bs=bs'; comp; meas=None; body } }
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.PP.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.SyntaxHelpers.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.NamedView.fsti.checked", "FStar.Stubs.Syntax.Syntax.fsti.checked", "FStar.Stubs.Reflection.V2.Builtins.fsti.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Reflection.Typing.fsti.checked", "FStar.Printf.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked" ], "interface_file": true, "source_file": "Pulse.Recursion.fst" }
[ { "abbrev": true, "full_module": "Pulse.Syntax.Printer", "short_module": "P" }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "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
g: Pulse.Typing.Env.env -> rng: FStar.Range.range -> nm_orig: Prims.string -> nm_aux: Prims.string -> d: Pulse.Syntax.Base.decl -> r_typ: FStar.Stubs.Reflection.Types.term -> blob: FStar.Reflection.Typing.blob -> FStar.Tactics.Effect.Tac (Prims.list (FStar.Reflection.Typing.sigelt_for (Pulse.Typing.Env.fstar_env g)))
FStar.Tactics.Effect.Tac
[]
[]
[ "Pulse.Typing.Env.env", "FStar.Range.range", "Prims.string", "Pulse.Syntax.Base.decl", "FStar.Stubs.Reflection.Types.typ", "FStar.Reflection.Typing.blob", "Prims.bool", "FStar.Stubs.Reflection.Types.sigelt", "FStar.Pervasives.Native.option", "Prims.Cons", "FStar.Reflection.Typing.sigelt_for", "Pulse.Typing.Env.fstar_env", "FStar.Pervasives.Native.Mktuple3", "FStar.Pervasives.Native.Some", "Prims.Nil", "Prims.eq2", "Pulse.RuntimeUtils.add_attribute", "FStar.Stubs.Reflection.Types.term", "Pulse.Recursion.string_as_term", "Prims.list", "FStar.Reflection.Typing.mk_unchecked_let", "FStar.Tactics.NamedView.term", "FStar.Tactics.NamedView.binder", "FStar.Tactics.NamedView.comp", "FStar.Tactics.V2.SyntaxHelpers.mk_arr", "Prims.unit", "Prims.uu___is_Nil", "Pulse.Typing.Env.fail", "Pulse.Syntax.Base.range", "FStar.List.Tot.Base.init", "FStar.Pervasives.Native.tuple2", "FStar.Tactics.V2.SyntaxHelpers.collect_arr_bs" ]
[]
false
true
false
false
false
let tie_knot (g: env) (rng: R.range) (nm_orig nm_aux: string) (d: decl) (r_typ: R.typ) (blob: RT.blob) : Tac (list (RT.sigelt_for (fstar_env g))) =
let knot_r_typ = let bs, c = collect_arr_bs r_typ in if Nil? bs then fail g (Some rng) "tie_knot: impossible (1)"; let bs = init bs in if Nil? bs then fail g (Some rng) "tie_knot: impossible (2)"; mk_arr bs c in let flag, sig, _ = RT.mk_unchecked_let (fstar_env g) nm_orig (`(magic ())) knot_r_typ in let nm = string_as_term nm_aux in let sig = RU.add_attribute sig (`("pulse.recursive.knot", (`#(nm)))) in [flag, sig, Some blob]
false
AlgWP.fst
AlgWP.lift_pure_algwp
val lift_pure_algwp (a: Type) (wp: _) (f: (unit -> PURE a wp)) : Pure (repr a noops (lift_pure_wp wp)) (requires (wp (fun _ -> True))) (ensures (fun _ -> True))
val lift_pure_algwp (a: Type) (wp: _) (f: (unit -> PURE a wp)) : Pure (repr a noops (lift_pure_wp wp)) (requires (wp (fun _ -> True))) (ensures (fun _ -> True))
let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 12, "end_line": 241, "start_col": 0, "start_line": 232 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: (_: Prims.unit -> Prims.PURE a) -> Prims.Pure (AlgWP.repr a AlgWP.noops (AlgWP.lift_pure_wp wp))
Prims.Pure
[]
[]
[ "Prims.pure_wp", "Prims.unit", "Alg.Return", "FStar.Pervasives.assert_norm", "AlgWP.stronger", "AlgWP.lift_pure_wp", "AlgWP.return_wp", "Prims._assert", "Prims.l_Forall", "Prims.l_imp", "FStar.Monotonic.Pure.elim_pure_wp_monotonicity", "FStar.Monotonic.Pure.elim_pure", "Prims.l_True", "AlgWP.repr", "AlgWP.noops" ]
[]
false
false
false
false
false
let lift_pure_algwp (a: Type) wp (f: (unit -> PURE a wp)) : Pure (repr a noops (lift_pure_wp wp)) (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) =
let v:a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; assert (forall p. wp p ==> p v); assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v
false
Pulse.Recursion.fst
Pulse.Recursion.freshen_binder
val freshen_binder (b: T.binder) : T.binder
val freshen_binder (b: T.binder) : T.binder
let freshen_binder (b:T.binder) : T.binder = { b with uniq = 10000 + b.uniq ; ppname = map_seal b.ppname (fun s -> s ^ "'") }
{ "file_name": "lib/steel/pulse/Pulse.Recursion.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 58, "end_line": 58, "start_col": 0, "start_line": 56 }
(* 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.Recursion module R = FStar.Reflection.V2 module RT = FStar.Reflection.Typing module T = FStar.Tactics.V2 open FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open FStar.List.Tot open Pulse.Syntax open Pulse.Typing module P = Pulse.Syntax.Printer exception Splitlast_empty let rec splitlast #a (l : list a) : Tac (list a & a) = match l with | [] -> raise Splitlast_empty | [x] -> [], x | x::xs -> let init, last = splitlast xs in x::init, last exception Map2_length_mismatch let rec map2 #a #b #c (f : a -> b -> Tac c) (xs : list a) (ys : list b) : Tac (list c) = match xs, ys with | [], [] -> [] | x::xx, y::yy -> f x y :: map2 f xx yy | _ -> raise Map2_length_mismatch let debug_main g (s: unit -> Tac string) : Tac unit = if RU.debug_at_level (fstar_env g) "pulse.main" then print (s ()) else () let string_as_term (s:string) : R.term = R.pack_ln (R.Tv_Const (C_String s))
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.PP.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.SyntaxHelpers.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.NamedView.fsti.checked", "FStar.Stubs.Syntax.Syntax.fsti.checked", "FStar.Stubs.Reflection.V2.Builtins.fsti.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Reflection.Typing.fsti.checked", "FStar.Printf.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked" ], "interface_file": true, "source_file": "Pulse.Recursion.fst" }
[ { "abbrev": true, "full_module": "Pulse.Syntax.Printer", "short_module": "P" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "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
b: FStar.Tactics.NamedView.binder -> FStar.Tactics.NamedView.binder
Prims.Tot
[ "total" ]
[]
[ "FStar.Tactics.NamedView.binder", "FStar.Tactics.NamedView.Mkbinder", "Prims.op_Addition", "FStar.Tactics.NamedView.__proj__Mkbinder__item__uniq", "FStar.Sealed.map_seal", "Prims.string", "FStar.Tactics.NamedView.__proj__Mkbinder__item__ppname", "Prims.op_Hat", "FStar.Tactics.NamedView.__proj__Mkbinder__item__sort", "FStar.Tactics.NamedView.__proj__Mkbinder__item__qual", "FStar.Tactics.NamedView.__proj__Mkbinder__item__attrs" ]
[]
false
false
false
true
false
let freshen_binder (b: T.binder) : T.binder =
{ b with uniq = 10000 + b.uniq; ppname = map_seal b.ppname (fun s -> s ^ "'") }
false
AlgWP.fst
AlgWP.quotient_ro
val quotient_ro (#a: _) (w: st_wp a) : st_wp a
val quotient_ro (#a: _) (w: st_wp a) : st_wp a
let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 272, "start_col": 0, "start_line": 271 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
w: AlgWP.st_wp a -> AlgWP.st_wp a
Prims.Tot
[ "total" ]
[]
[ "AlgWP.st_wp", "Alg.state", "FStar.Pervasives.Native.tuple2", "Prims.logical", "Prims.l_imp", "Prims.eq2", "FStar.Pervasives.Native.Mktuple2" ]
[]
false
false
false
true
false
let quotient_ro #a (w: st_wp a) : st_wp a =
fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1))
false
AlgWP.fst
AlgWP.put
val put (s: state) : AlgWP unit [Write] (write_wp s)
val put (s: state) : AlgWP unit [Write] (write_wp s)
let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return)
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 36, "end_line": 225, "start_col": 0, "start_line": 224 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: Alg.state -> AlgWP.AlgWP Prims.unit
AlgWP.AlgWP
[]
[]
[ "Alg.state", "Alg.Op", "Prims.unit", "Alg.Write", "Alg.Return", "Prims.Cons", "Alg.op", "Prims.Nil", "AlgWP.write_wp" ]
[]
false
true
false
false
false
let put (s: state) : AlgWP unit [Write] (write_wp s) =
AlgWP?.reflect (Op Write s Return)
false
AlgWP.fst
AlgWP.addx
val addx (x: int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0 + x)))
val addx (x: int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0 + x)))
let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y)
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 11, "end_line": 247, "start_col": 0, "start_line": 245 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: Prims.int -> AlgWP.AlgWP Prims.unit
AlgWP.AlgWP
[]
[]
[ "Prims.int", "AlgWP.put", "Prims.op_Addition", "Prims.unit", "Alg.state", "AlgWP.get", "Prims.Cons", "Alg.op", "Alg.Read", "Alg.Write", "Prims.Nil", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2" ]
[]
false
true
false
false
false
let addx (x: int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0 + x))) =
let y = get () in put (x + y)
false
Pulse.Checker.Prover.ElimExists.fst
Pulse.Checker.Prover.ElimExists.mk
val mk (#g: env) (#v: vprop) (v_typing: tot_typing g v tm_vprop) : T.Tac (option (x: ppname & t: st_term & c: comp{stateful_comp c /\ comp_pre c == v} & st_typing g t c) )
val mk (#g: env) (#v: vprop) (v_typing: tot_typing g v tm_vprop) : T.Tac (option (x: ppname & t: st_term & c: comp{stateful_comp c /\ comp_pre c == v} & st_typing g t c) )
let mk (#g:env) (#v:vprop) (v_typing:tot_typing g v tm_vprop) : T.Tac (option (x:ppname & t:st_term & c:comp { stateful_comp c /\ comp_pre c == v } & st_typing g t c)) = match v.t with | Tm_ExistsSL u { binder_ppname=nm; binder_ty = t } p -> let x = fresh g in let c = Pulse.Typing.comp_elim_exists u t p (nm, x) in let tm_typing : st_typing g _ c = T_ElimExists g (comp_u c) t p x (RU.magic()) (RU.magic()) in Some (| nm, _, c, tm_typing |) | _ -> None
{ "file_name": "lib/steel/pulse/Pulse.Checker.Prover.ElimExists.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 13, "end_line": 49, "start_col": 0, "start_line": 35 }
(* 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.Checker.Prover.ElimExists open Pulse.Syntax open Pulse.Typing open Pulse.Typing.Combinators module T = FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open Pulse.Checker.VPropEquiv open Pulse.Checker.Prover.Base let should_elim_exists (v:vprop) : T.Tac bool = match v.t with | Tm_ExistsSL _ _ _ -> true | _ -> false
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.Combinators.fsti.checked", "Pulse.Typing.fst.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.Checker.VPropEquiv.fsti.checked", "Pulse.Checker.Prover.Base.fsti.checked", "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked" ], "interface_file": true, "source_file": "Pulse.Checker.Prover.ElimExists.fst" }
[ { "abbrev": false, "full_module": "Pulse.Checker.Prover.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.VPropEquiv", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "Pulse.Typing.Combinators", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "Pulse.Checker.Prover.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Base", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Prover", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Checker.Prover", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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_typing: Pulse.Typing.tot_typing g v Pulse.Syntax.Base.tm_vprop -> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option (FStar.Pervasives.dtuple4 Pulse.Syntax.Base.ppname (fun _ -> Pulse.Syntax.Base.st_term) (fun _ _ -> c: Pulse.Syntax.Base.comp {Pulse.Syntax.Base.stateful_comp c /\ Pulse.Syntax.Base.comp_pre c == v}) (fun _ t c -> Pulse.Typing.st_typing g t c)))
FStar.Tactics.Effect.Tac
[]
[]
[ "Pulse.Typing.Env.env", "Pulse.Syntax.Base.vprop", "Pulse.Typing.tot_typing", "Pulse.Syntax.Base.tm_vprop", "Pulse.Syntax.Base.__proj__Mkterm__item__t", "Pulse.Syntax.Base.universe", "Pulse.Syntax.Base.term", "Pulse.Syntax.Base.ppname", "FStar.Sealed.Inhabited.sealed", "Prims.list", "Prims.Nil", "FStar.Pervasives.Native.Some", "FStar.Pervasives.dtuple4", "Pulse.Syntax.Base.st_term", "Pulse.Syntax.Base.comp", "Prims.l_and", "Prims.b2t", "Pulse.Syntax.Base.stateful_comp", "Prims.eq2", "Pulse.Syntax.Base.comp_pre", "Pulse.Typing.st_typing", "FStar.Pervasives.Mkdtuple4", "Pulse.Typing.wtag", "Pulse.Syntax.Base.ctag", "Pulse.Syntax.Base.STT_Ghost", "Pulse.Syntax.Base.Tm_ElimExists", "Pulse.Syntax.Base.Mkst_term'__Tm_ElimExists__payload", "Pulse.Syntax.Base.tm_exists_sl", "Pulse.Syntax.Base.comp_u", "Pulse.Syntax.Base.as_binder", "Pulse.Typing.T_ElimExists", "Pulse.RuntimeUtils.magic", "Pulse.Syntax.Pure.tm_type", "Pulse.Typing.comp_elim_exists", "FStar.Pervasives.Native.Mktuple2", "Pulse.Syntax.Base.var", "Prims.l_not", "FStar.Set.mem", "Pulse.Typing.Env.dom", "Pulse.Typing.Env.fresh", "Pulse.Syntax.Base.term'", "FStar.Pervasives.Native.None", "FStar.Pervasives.Native.option" ]
[]
false
true
false
false
false
let mk (#g: env) (#v: vprop) (v_typing: tot_typing g v tm_vprop) : T.Tac (option (x: ppname & t: st_term & c: comp{stateful_comp c /\ comp_pre c == v} & st_typing g t c) ) =
match v.t with | Tm_ExistsSL u { binder_ppname = nm ; binder_ty = t } p -> let x = fresh g in let c = Pulse.Typing.comp_elim_exists u t p (nm, x) in let tm_typing:st_typing g _ c = T_ElimExists g (comp_u c) t p x (RU.magic ()) (RU.magic ()) in Some (| nm, _, c, tm_typing |) | _ -> None
false
AlgWP.fst
AlgWP.null_ro
val null_ro (#a: _) : st_wp a
val null_ro (#a: _) : st_wp a
let null_ro #a : st_wp a = quotient_ro null
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 43, "end_line": 358, "start_col": 0, "start_line": 358 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x)) let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c let quot #a #wp (f : unit -> AlgWP a [Read] wp) : AlgWP a [Read] (quotient_ro wp) = AlgWP?.reflect (quot_tree (reify (f ()))) effect AlgPP (a:Type) (ll:rwops) (pre : state -> Type0) (post : state -> a -> state -> Type0) = AlgWP a ll (fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) let quotPP #a #pre #post (f : unit -> AlgPP a [Read] pre post) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1) = quot #_ #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
AlgWP.st_wp a
Prims.Tot
[ "total" ]
[]
[ "AlgWP.quotient_ro", "AlgWP.null", "AlgWP.st_wp" ]
[]
false
false
false
true
false
let null_ro #a : st_wp a =
quotient_ro null
false
AlgWP.fst
AlgWP.st_soundness_aux
val st_soundness_aux (#a #wp: _) (t: repr a [Read; Write] wp) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (wp s0)))
val st_soundness_aux (#a #wp: _) (t: repr a [Read; Write] wp) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (wp s0)))
let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 16, "end_line": 293, "start_col": 0, "start_line": 291 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: AlgWP.repr a [Alg.Read; Alg.Write] wp -> s0: Alg.state -> ID5.ID (a * Alg.state)
ID5.ID
[]
[]
[ "AlgWP.st_wp", "AlgWP.repr", "Prims.Cons", "Alg.op", "Alg.Read", "Alg.Write", "Prims.Nil", "AlgWP.interp_sem", "Alg.state", "FStar.Pervasives.Native.tuple2", "FStar.Monotonic.Pure.as_pure_wp" ]
[]
false
true
false
false
false
let st_soundness_aux #a #wp (t: repr a [Read; Write] wp) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (wp s0))) =
interp_sem t
false
AlgWP.fst
AlgWP.sanity_1
val sanity_1 : Prims.unit
let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p)
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 79, "end_line": 279, "start_col": 0, "start_line": 279 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Prims.unit
Prims.Tot
[ "total" ]
[]
[ "Prims._assert", "Prims.l_Forall", "Alg.state", "FStar.Pervasives.Native.tuple2", "Prims.l_iff", "AlgWP.quotient_ro", "AlgWP.read_wp" ]
[]
false
false
false
true
false
let sanity_1 =
assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p)
false
AlgWP.fst
AlgWP.add_via_state
val add_via_state (x y: int) : AlgWP int [Read; Write] (fun s0 p -> p ((x + y), s0))
val add_via_state (x y: int) : AlgWP int [Read; Write] (fun s0 p -> p ((x + y), s0))
let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 258, "start_col": 0, "start_line": 252 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: Prims.int -> y: Prims.int -> AlgWP.AlgWP Prims.int
AlgWP.AlgWP
[]
[]
[ "Prims.int", "Prims.unit", "AlgWP.put", "Alg.state", "AlgWP.get", "AlgWP.addx", "Prims.Cons", "Alg.op", "Alg.Read", "Alg.Write", "Prims.Nil", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Prims.op_Addition" ]
[]
false
true
false
false
false
let add_via_state (x y: int) : AlgWP int [Read; Write] (fun s0 p -> p ((x + y), s0)) =
let o = get () in put x; addx y; let r = get () in put o; r
false
AlgWP.fst
AlgWP.sanity_2
val sanity_2 : Prims.unit
let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p)
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 85, "end_line": 280, "start_col": 0, "start_line": 280 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Prims.unit
Prims.Tot
[ "total" ]
[]
[ "Prims._assert", "Prims.l_Forall", "Alg.state", "FStar.Pervasives.Native.tuple2", "Prims.unit", "Prims.l_imp", "FStar.Pervasives.Native.Mktuple2", "AlgWP.quotient_ro", "AlgWP.write_wp" ]
[]
false
false
false
true
false
let sanity_2 =
assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p)
false
AlgWP.fst
AlgWP.st_soundness
val st_soundness (#a #wp: _) (t: (unit -> AlgWP a [Read; Write] wp)) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (wp s0)))
val st_soundness (#a #wp: _) (t: (unit -> AlgWP a [Read; Write] wp)) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (wp s0)))
let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ()))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 35, "end_line": 297, "start_col": 0, "start_line": 295 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: (_: Prims.unit -> AlgWP.AlgWP a) -> s0: Alg.state -> ID5.ID (a * Alg.state)
ID5.ID
[]
[]
[ "AlgWP.st_wp", "Prims.unit", "Prims.Cons", "Alg.op", "Alg.Read", "Alg.Write", "Prims.Nil", "AlgWP.st_soundness_aux", "Alg.state", "FStar.Pervasives.Native.tuple2", "FStar.Monotonic.Pure.as_pure_wp" ]
[]
false
true
false
false
false
let st_soundness #a #wp (t: (unit -> AlgWP a [Read; Write] wp)) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (wp s0))) =
st_soundness_aux (reify (t ()))
false
AlgWP.fst
AlgWP.interp_sem
val interp_sem (#a: _) (t: rwtree a [Read; Write]) (s0: state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0))
val interp_sem (#a: _) (t: rwtree a [Read; Write]) (s0: state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0))
let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 25, "end_line": 269, "start_col": 0, "start_line": 262 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: AlgWP.rwtree a [Alg.Read; Alg.Write] -> s0: Alg.state -> ID5.ID (a * Alg.state)
ID5.ID
[]
[]
[ "AlgWP.rwtree", "Prims.Cons", "Alg.op", "Alg.Read", "Alg.Write", "Prims.Nil", "Alg.state", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.tuple2", "Alg.op_inp", "Alg.op_out", "Alg.tree0", "AlgWP.interp_sem", "FStar.Monotonic.Pure.as_pure_wp", "AlgWP.interp_as_wp" ]
[ "recursion" ]
false
true
false
false
false
let rec interp_sem #a (t: rwtree a [Read; Write]) (s0: state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) =
match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i
false
AlgWP.fst
AlgWP.ro_soundness
val ro_soundness (#a #wp: _) ($t: (unit -> AlgWP a [Read] wp)) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0)))
val ro_soundness (#a #wp: _) ($t: (unit -> AlgWP a [Read] wp)) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0)))
let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ()))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 35, "end_line": 308, "start_col": 0, "start_line": 306 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: (_: Prims.unit -> AlgWP.AlgWP a) -> s0: Alg.state -> ID5.ID (a * Alg.state)
ID5.ID
[]
[]
[ "AlgWP.st_wp", "Prims.unit", "Prims.Cons", "Alg.op", "Alg.Read", "Prims.Nil", "AlgWP.ro_soundness_aux", "Alg.state", "FStar.Pervasives.Native.tuple2", "FStar.Monotonic.Pure.as_pure_wp", "AlgWP.quotient_ro" ]
[]
false
true
false
false
false
let ro_soundness #a #wp ($t: (unit -> AlgWP a [Read] wp)) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) =
ro_soundness_aux (reify (t ()))
false
AlgWP.fst
AlgWP.ro_soundness_aux
val ro_soundness_aux (#a #wp: _) ($t: repr a [Read] wp) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0)))
val ro_soundness_aux (#a #wp: _) ($t: repr a [Read] wp) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0)))
let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 15, "end_line": 304, "start_col": 0, "start_line": 302 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: AlgWP.repr a [Alg.Read] wp -> s0: Alg.state -> ID5.ID (a * Alg.state)
ID5.ID
[]
[]
[ "AlgWP.st_wp", "AlgWP.repr", "Prims.Cons", "Alg.op", "Alg.Read", "Prims.Nil", "AlgWP.interp_ro", "Alg.state", "FStar.Pervasives.Native.tuple2", "FStar.Monotonic.Pure.as_pure_wp", "AlgWP.quotient_ro" ]
[]
false
true
false
false
false
let ro_soundness_aux #a #wp ($t: repr a [Read] wp) : Tot (s0: state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) =
interp_ro t
false
AlgWP.fst
AlgWP.interp_ro
val interp_ro (#a: _) (t: rwtree a [Read]) (s0: state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0))
val interp_ro (#a: _) (t: rwtree a [Read]) (s0: state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0))
let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 25, "end_line": 288, "start_col": 0, "start_line": 283 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_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
t: AlgWP.rwtree a [Alg.Read] -> s0: Alg.state -> ID5.ID (a * Alg.state)
ID5.ID
[]
[]
[ "AlgWP.rwtree", "Prims.Cons", "Alg.op", "Alg.Read", "Prims.Nil", "Alg.state", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.tuple2", "Alg.op_inp", "Alg.op_out", "Alg.tree0", "AlgWP.interp_ro", "FStar.Monotonic.Pure.as_pure_wp", "AlgWP.quotient_ro", "AlgWP.interp_as_wp" ]
[ "recursion" ]
false
true
false
false
false
let rec interp_ro #a (t: rwtree a [Read]) (s0: state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) =
match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0
false
AlgWP.fst
AlgWP.ro_soundness_pre_post
val ro_soundness_pre_post (#a #wp: _) (t: (unit -> AlgWP a [Read] wp)) (s0: state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1))
val ro_soundness_pre_post (#a #wp: _) (t: (unit -> AlgWP a [Read] wp)) (s0: state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1))
let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 21, "end_line": 318, "start_col": 0, "start_line": 314 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: (_: Prims.unit -> AlgWP.AlgWP a) -> s0: Alg.state -> ID5.Id (a * Alg.state)
ID5.Id
[]
[]
[ "AlgWP.st_wp", "Prims.unit", "Prims.Cons", "Alg.op", "Alg.Read", "Prims.Nil", "Alg.state", "AlgWP.ro_soundness", "FStar.Pervasives.Native.tuple2", "Prims.l_True", "Prims.eq2" ]
[]
false
true
false
false
false
let ro_soundness_pre_post #a #wp (t: (unit -> AlgWP a [Read] wp)) (s0: state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) =
ro_soundness t s0
false
AlgWP.fst
AlgWP.null_ro1
val null_ro1 (#a: _) : st_wp a
val null_ro1 (#a: _) : st_wp a
let null_ro1 #a : st_wp a = fun s0 p -> forall x. p (x, s0)
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 59, "end_line": 359, "start_col": 0, "start_line": 359 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x)) let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c let quot #a #wp (f : unit -> AlgWP a [Read] wp) : AlgWP a [Read] (quotient_ro wp) = AlgWP?.reflect (quot_tree (reify (f ()))) effect AlgPP (a:Type) (ll:rwops) (pre : state -> Type0) (post : state -> a -> state -> Type0) = AlgWP a ll (fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) let quotPP #a #pre #post (f : unit -> AlgPP a [Read] pre post) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1) = quot #_ #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f let null #a : st_wp a = fun s0 p -> forall r. p r
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
AlgWP.st_wp a
Prims.Tot
[ "total" ]
[]
[ "Alg.state", "FStar.Pervasives.Native.tuple2", "Prims.l_Forall", "FStar.Pervasives.Native.Mktuple2", "Prims.logical", "AlgWP.st_wp" ]
[]
false
false
false
true
false
let null_ro1 #a : st_wp a =
fun s0 p -> forall x. p (x, s0)
false
Pulse.Recursion.fst
Pulse.Recursion.add_knot
val add_knot (g : env) (rng : R.range) (d : decl) : Tac decl
val add_knot (g : env) (rng : R.range) (d : decl) : Tac decl
let add_knot (g : env) (rng : R.range) (d : decl{FnDecl? d.d}) : Tac decl = let FnDecl { id; isrec; bs; comp; meas; body } = d.d in if Nil? bs then fail g (Some d.range) "main: FnDecl does not have binders"; (* NB: bs and comp are open *) let r_res = elab_comp comp in debug_main g (fun _ -> Printf.sprintf "add_knot: bs = %s\n" (string_of_list (fun (_, b,_) -> P.binder_to_string b) bs)); (* for fn rec f (x1:t1) ... (xn:tn) : requires pre returns x:a ensures post we elab into let f (x1:t1) ... (xn:tn) (f : (x1':t1) -> ... -> (xn':tn) -> stt a pre post) : stt a pre post without any sort of termination check. Now, for ghost fn rec f (x1:t1) ... (xn:tn) : requires pre returns x:a ensures post measure meas we must elab into let f (x1:t1) ... (xn:tn) (f : (x1':t1) -> ... -> (xn':tn){meas' << meas} -> stt a pre post) : stt a pre post so we need to add the measure refinement. Since `meas` is an open term (wrt x1...xn), we must substitute it to create meas', subtituting x1' for x1, ..., xn' for xn *) (* Desugaring added a recursive knot argument at the end *) let bs, b_knot = splitlast bs in (* freshen *) let r_bs0 = List.Tot.map elab_b bs in let r_bs = freshen_binders r_bs0 in let binder_to_r_namedv (b:T.binder) : R.namedv = R.pack_namedv { uniq = b.uniq; sort = seal b.sort; ppname = b.ppname; } in let prime_subst = map2 (fun (b1 b2 : T.binder) -> R.NT (binder_to_r_namedv b1) (binder_to_term b2)) r_bs0 r_bs in let r_bs = (* If ghost/atomic, we need to add a decreases refinement on the last arg *) if C_STAtomic? comp || C_STGhost? comp then ( if None? meas then ( let open FStar.Stubs.Pprint in let open Pulse.PP in fail_doc g (Some d.range) [ text "'ghost' and 'atomic' recursive functions require a 'decreases' clause"] ); let init, last = splitlast r_bs in let last : FStar.Tactics.NamedView.binder = last in let last = (* add a refinement to last *) let b' : simple_binder = { uniq = last.uniq; ppname = last.ppname; sort = last.sort; qual = Q_Explicit; attrs = []; } in let meas = Some?.v meas in let meas = elab_term meas in let meas' = R.subst_term prime_subst meas in let ref = `(`#meas' << `#meas) in (* TODO: this is not always printed *) let ref = (`labeled range_0 "Could not prove termination" (`#ref)) in { last with sort = (pack (Tv_Refine b' ref)) } in init @ [last] ) else r_bs in let r_res = R.subst_term prime_subst r_res in let r_ty = FStar.Tactics.V2.SyntaxHelpers.mk_tot_arr r_bs r_res in // let open FStar.Stubs.Pprint in // let open Pulse.PP in // warn_doc g (Some d.range) [ // text "r_ty (type of the knot binder) =" ^/^ pp r_ty // ]; if R.Tv_Unknown? (inspect_ln r_ty) then fail g (Some d.range) "error: r_ty is Tv_unknown in add_knot?"; let b_knot = let s, rng = inspect_ident id in let b = mk_binder s rng (tm_fstar r_ty rng) in let bv = { bv_index = b_knot._3.bv_index; bv_ppname = { name = seal s; range = rng } } in let q = None in (q, b, bv) in let id' = let s, rng = inspect_ident id in pack_ident ("__recaux_" ^ s, rng) in let bs' = bs @ [b_knot] in (* NB: body and comp unchanged, they are already shifted properly (we dropped one binder and added one) *) { d with d = FnDecl { id=id'; isrec=false; bs=bs'; comp; meas=None; body } }
{ "file_name": "lib/steel/pulse/Pulse.Recursion.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 3, "end_line": 205, "start_col": 0, "start_line": 82 }
(* 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.Recursion module R = FStar.Reflection.V2 module RT = FStar.Reflection.Typing module T = FStar.Tactics.V2 open FStar.Tactics.V2 module RU = Pulse.RuntimeUtils open FStar.List.Tot open Pulse.Syntax open Pulse.Typing module P = Pulse.Syntax.Printer exception Splitlast_empty let rec splitlast #a (l : list a) : Tac (list a & a) = match l with | [] -> raise Splitlast_empty | [x] -> [], x | x::xs -> let init, last = splitlast xs in x::init, last exception Map2_length_mismatch let rec map2 #a #b #c (f : a -> b -> Tac c) (xs : list a) (ys : list b) : Tac (list c) = match xs, ys with | [], [] -> [] | x::xx, y::yy -> f x y :: map2 f xx yy | _ -> raise Map2_length_mismatch let debug_main g (s: unit -> Tac string) : Tac unit = if RU.debug_at_level (fstar_env g) "pulse.main" then print (s ()) else () let string_as_term (s:string) : R.term = R.pack_ln (R.Tv_Const (C_String s)) let freshen_binder (b:T.binder) : T.binder = { b with uniq = 10000 + b.uniq ; ppname = map_seal b.ppname (fun s -> s ^ "'") } let subst_binder_typ (s : FStar.Stubs.Syntax.Syntax.subst_t) (b : Tactics.NamedView.binder) : Tactics.NamedView.binder = { b with sort = FStar.Stubs.Reflection.V2.Builtins.subst_term s b.sort } let rec freshen_binders (bs:binders) : Tot binders (decreases length bs) = match bs with | [] -> [] | b::bs -> let b' = freshen_binder b in let bs = map (subst_binder_typ [Stubs.Syntax.Syntax.NT (binder_to_namedv b |> FStar.Stubs.Reflection.V2.Builtins.pack_namedv) (binder_to_term b')]) bs in b' :: freshen_binders bs let elab_b (qbv : option qualifier & binder & bv) : Tot Tactics.NamedView.binder = let q, b, bv = qbv in { uniq = bv.bv_index; ppname = b.binder_ppname.name; sort = elab_term b.binder_ty; qual = elab_qual q; attrs = []; }
{ "checked_file": "/", "dependencies": [ "Pulse.Typing.fst.checked", "Pulse.Syntax.Printer.fsti.checked", "Pulse.Syntax.fst.checked", "Pulse.RuntimeUtils.fsti.checked", "Pulse.PP.fst.checked", "prims.fst.checked", "FStar.Tactics.V2.SyntaxHelpers.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.NamedView.fsti.checked", "FStar.Stubs.Syntax.Syntax.fsti.checked", "FStar.Stubs.Reflection.V2.Builtins.fsti.checked", "FStar.Stubs.Pprint.fsti.checked", "FStar.Reflection.V2.fst.checked", "FStar.Reflection.Typing.fsti.checked", "FStar.Printf.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.List.Tot.fst.checked" ], "interface_file": true, "source_file": "Pulse.Recursion.fst" }
[ { "abbrev": true, "full_module": "Pulse.Syntax.Printer", "short_module": "P" }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": true, "full_module": "Pulse.RuntimeUtils", "short_module": "RU" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "Pulse.Typing", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Syntax", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.Reflection.Typing", "short_module": "RT" }, { "abbrev": true, "full_module": "FStar.Reflection.V2", "short_module": "R" }, { "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
g: Pulse.Typing.Env.env -> rng: FStar.Range.range -> d: Pulse.Syntax.Base.decl -> FStar.Tactics.Effect.Tac Pulse.Syntax.Base.decl
FStar.Tactics.Effect.Tac
[]
[]
[ "Pulse.Typing.Env.env", "FStar.Range.range", "Pulse.Syntax.Base.decl", "Prims.b2t", "Pulse.Syntax.Base.uu___is_FnDecl", "Pulse.Syntax.Base.__proj__Mkdecl__item__d", "FStar.Stubs.Reflection.Types.ident", "Prims.bool", "Prims.list", "FStar.Pervasives.Native.tuple3", "FStar.Pervasives.Native.option", "Pulse.Syntax.Base.qualifier", "Pulse.Syntax.Base.binder", "Pulse.Syntax.Base.bv", "Pulse.Syntax.Base.comp", "Pulse.Syntax.Base.term", "Prims.l_imp", "FStar.Pervasives.Native.uu___is_Some", "Pulse.Syntax.Base.st_term", "Pulse.Syntax.Base.Mkdecl", "Pulse.Syntax.Base.FnDecl", "Pulse.Syntax.Base.Mkdecl'__FnDecl__payload", "FStar.Pervasives.Native.None", "Pulse.Syntax.Base.__proj__Mkdecl__item__range", "FStar.List.Tot.Base.op_At", "Prims.Cons", "Prims.Nil", "Prims.string", "FStar.Stubs.Reflection.V2.Builtins.pack_ident", "FStar.Pervasives.Native.Mktuple2", "Prims.op_Hat", "FStar.Stubs.Reflection.V2.Data.ident_view", "Prims.precedes", "FStar.Stubs.Reflection.V2.Builtins.inspect_ident", "FStar.Pervasives.Native.Mktuple3", "Pulse.Syntax.Base.Mkbv", "Pulse.Syntax.Base.__proj__Mkbv__item__bv_index", "FStar.Pervasives.Native.__proj__Mktuple3__item___3", "Pulse.Syntax.Base.Mkppname", "FStar.Sealed.seal", "Pulse.Syntax.Base.mk_binder", "Pulse.Syntax.Base.tm_fstar", "Prims.unit", "FStar.Stubs.Reflection.V2.Data.uu___is_Tv_Unknown", "FStar.Stubs.Reflection.V2.Builtins.inspect_ln", "Pulse.Typing.Env.fail", "FStar.Pervasives.Native.Some", "Pulse.Syntax.Base.range", "FStar.Tactics.NamedView.term", "FStar.Tactics.V2.SyntaxHelpers.mk_tot_arr", "FStar.Stubs.Reflection.Types.term", "FStar.Stubs.Reflection.V2.Builtins.subst_term", "FStar.Tactics.NamedView.binder", "Prims.op_BarBar", "Pulse.Syntax.Base.uu___is_C_STAtomic", "Pulse.Syntax.Base.uu___is_C_STGhost", "FStar.Tactics.NamedView.Mkbinder", "FStar.Tactics.NamedView.__proj__Mkbinder__item__uniq", "FStar.Tactics.NamedView.__proj__Mkbinder__item__ppname", "FStar.Tactics.NamedView.pack", "FStar.Tactics.NamedView.Tv_Refine", "FStar.Tactics.NamedView.__proj__Mkbinder__item__qual", "FStar.Tactics.NamedView.__proj__Mkbinder__item__attrs", "Pulse.Elaborate.Pure.elab_term", "FStar.Pervasives.Native.__proj__Some__item__v", "FStar.Tactics.NamedView.simple_binder", "FStar.Tactics.NamedView.__proj__Mkbinder__item__sort", "FStar.Stubs.Reflection.V2.Data.Q_Explicit", "FStar.Pervasives.Native.tuple2", "Pulse.Recursion.splitlast", "FStar.Pervasives.Native.uu___is_None", "Pulse.Typing.Env.fail_doc", "FStar.Stubs.Pprint.document", "Pulse.PP.text", "FStar.Stubs.Syntax.Syntax.subst_elt", "Pulse.Recursion.map2", "FStar.Stubs.Syntax.Syntax.NT", "FStar.Tactics.V2.SyntaxCoercions.binder_to_term", "FStar.Stubs.Reflection.Types.namedv", "FStar.Stubs.Reflection.V2.Builtins.pack_namedv", "FStar.Stubs.Reflection.V2.Data.Mknamedv_view", "FStar.Stubs.Reflection.Types.typ", "FStar.Tactics.NamedView.binders", "Pulse.Recursion.freshen_binders", "FStar.List.Tot.Base.map", "Pulse.Recursion.elab_b", "Pulse.Recursion.debug_main", "FStar.Printf.sprintf", "FStar.Tactics.Util.string_of_list", "Pulse.Syntax.Printer.binder_to_string", "Pulse.Elaborate.Pure.elab_comp", "Prims.uu___is_Nil", "Pulse.Syntax.Base.decl'" ]
[]
false
true
false
false
false
let add_knot (g: env) (rng: R.range) (d: decl{FnDecl? d.d}) : Tac decl =
let FnDecl { id = id ; isrec = isrec ; bs = bs ; comp = comp ; meas = meas ; body = body } = d.d in if Nil? bs then fail g (Some d.range) "main: FnDecl does not have binders"; let r_res = elab_comp comp in debug_main g (fun _ -> Printf.sprintf "add_knot: bs = %s\n" (string_of_list (fun (_, b, _) -> P.binder_to_string b) bs)); let bs, b_knot = splitlast bs in let r_bs0 = List.Tot.map elab_b bs in let r_bs = freshen_binders r_bs0 in let binder_to_r_namedv (b: T.binder) : R.namedv = R.pack_namedv ({ uniq = b.uniq; sort = seal b.sort; ppname = b.ppname }) in let prime_subst = map2 (fun (b1: T.binder) (b2: T.binder) -> R.NT (binder_to_r_namedv b1) (binder_to_term b2)) r_bs0 r_bs in let r_bs = if C_STAtomic? comp || C_STGhost? comp then (if None? meas then (let open FStar.Stubs.Pprint in let open Pulse.PP in fail_doc g (Some d.range) [text "'ghost' and 'atomic' recursive functions require a 'decreases' clause"]); let init, last = splitlast r_bs in let last:FStar.Tactics.NamedView.binder = last in let last = let b':simple_binder = { uniq = last.uniq; ppname = last.ppname; sort = last.sort; qual = Q_Explicit; attrs = [] } in let meas = Some?.v meas in let meas = elab_term meas in let meas' = R.subst_term prime_subst meas in let ref = `((`#meas') << (`#meas)) in let ref = (`labeled range_0 "Could not prove termination" (`#ref)) in { last with sort = (pack (Tv_Refine b' ref)) } in init @ [last]) else r_bs in let r_res = R.subst_term prime_subst r_res in let r_ty = FStar.Tactics.V2.SyntaxHelpers.mk_tot_arr r_bs r_res in if R.Tv_Unknown? (inspect_ln r_ty) then fail g (Some d.range) "error: r_ty is Tv_unknown in add_knot?"; let b_knot = let s, rng = inspect_ident id in let b = mk_binder s rng (tm_fstar r_ty rng) in let bv = { bv_index = b_knot._3.bv_index; bv_ppname = { name = seal s; range = rng } } in let q = None in (q, b, bv) in let id' = let s, rng = inspect_ident id in pack_ident ("__recaux_" ^ s, rng) in let bs' = bs @ [b_knot] in { d with d = FnDecl ({ id = id'; isrec = false; bs = bs'; comp = comp; meas = None; body = body }) }
false
AlgWP.fst
AlgWP.null_equiv_sanity
val null_equiv_sanity : a: Type -> Prims.unit
let null_equiv_sanity a = assert (null_ro #a `equiv` null_ro1 #a)
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 65, "end_line": 360, "start_col": 0, "start_line": 360 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x)) let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c let quot #a #wp (f : unit -> AlgWP a [Read] wp) : AlgWP a [Read] (quotient_ro wp) = AlgWP?.reflect (quot_tree (reify (f ()))) effect AlgPP (a:Type) (ll:rwops) (pre : state -> Type0) (post : state -> a -> state -> Type0) = AlgWP a ll (fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) let quotPP #a #pre #post (f : unit -> AlgPP a [Read] pre post) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1) = quot #_ #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f let null #a : st_wp a = fun s0 p -> forall r. p r let null_ro #a : st_wp a = quotient_ro null
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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 -> Prims.unit
Prims.Tot
[ "total" ]
[]
[ "Prims._assert", "AlgWP.equiv", "AlgWP.null_ro", "AlgWP.null_ro1", "Prims.unit" ]
[]
false
false
false
true
false
let null_equiv_sanity a =
assert ((null_ro #a) `equiv` (null_ro1 #a))
false
AlgWP.fst
AlgWP.quot_tree
val quot_tree (#a #wp: _) (c: repr a [Read] wp) : repr a [Read] (quotient_ro wp)
val quot_tree (#a #wp: _) (c: repr a [Read] wp) : repr a [Read] (quotient_ro wp)
let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 344, "start_col": 0, "start_line": 341 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: AlgWP.repr a [Alg.Read] wp -> AlgWP.repr a [Alg.Read] (AlgWP.quotient_ro wp)
Prims.Tot
[ "total" ]
[]
[ "AlgWP.st_wp", "AlgWP.repr", "Prims.Cons", "Alg.op", "Alg.Read", "Prims.Nil", "Prims.unit", "AlgWP.ro_tree_wp", "AlgWP.quotient_ro" ]
[]
false
false
false
false
false
let quot_tree #a #wp (c: repr a [Read] wp) : repr a [Read] (quotient_ro wp) =
ro_tree_wp c; c
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_abc_is_acb
val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b)
val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b)
let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 35, "end_line": 21, "start_col": 0, "start_line": 18 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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: Prims.nat -> b: Prims.nat -> c: Prims.nat -> FStar.Pervasives.Lemma (ensures (a * b) * c = (a * c) * b)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "FStar.Math.Lemmas.paren_mul_right", "Prims.unit", "FStar.Math.Lemmas.swap_mul" ]
[]
true
false
true
false
false
let lemma_abc_is_acb a b c =
Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b
false
AlgWP.fst
AlgWP.ro_tree_wp
val ro_tree_wp (#a: _) (t: tree a [Read]) : Lemma (is_ro (interp_as_wp t))
val ro_tree_wp (#a: _) (t: tree a [Read]) : Lemma (is_ro (interp_as_wp t))
let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 51, "end_line": 339, "start_col": 0, "start_line": 330 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: Alg.tree a [Alg.Read] -> FStar.Pervasives.Lemma (ensures AlgWP.is_ro (AlgWP.interp_as_wp t))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Alg.tree", "Prims.Cons", "Alg.op", "Alg.Read", "Prims.Nil", "Alg.op_inp", "Alg.op_out", "Alg.tree0", "AlgWP.bind_ro", "Alg.state", "AlgWP.read_wp", "AlgWP.interp_as_wp", "AlgWP.st_wp", "Prims.unit", "FStar.Classical.forall_intro", "AlgWP.is_ro", "Prims.l_True", "Prims.squash", "FStar.Pervasives.pattern", "AlgWP.ro_tree_wp" ]
[ "recursion" ]
false
false
true
false
false
let rec ro_tree_wp #a (t: tree a [Read]) : Lemma (is_ro (interp_as_wp t)) =
match t with | Return x -> () | Op Read i k -> let aux (x: state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x))
false
AlgWP.fst
AlgWP.quot
val quot (#a #wp: _) (f: (unit -> AlgWP a [Read] wp)) : AlgWP a [Read] (quotient_ro wp)
val quot (#a #wp: _) (f: (unit -> AlgWP a [Read] wp)) : AlgWP a [Read] (quotient_ro wp)
let quot #a #wp (f : unit -> AlgWP a [Read] wp) : AlgWP a [Read] (quotient_ro wp) = AlgWP?.reflect (quot_tree (reify (f ())))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 348, "start_col": 0, "start_line": 346 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x)) let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: Prims.unit -> AlgWP.AlgWP a) -> AlgWP.AlgWP a
AlgWP.AlgWP
[]
[]
[ "AlgWP.st_wp", "Prims.unit", "Prims.Cons", "Alg.op", "Alg.Read", "Prims.Nil", "AlgWP.quot_tree", "AlgWP.quotient_ro" ]
[]
false
true
false
false
false
let quot #a #wp (f: (unit -> AlgWP a [Read] wp)) : AlgWP a [Read] (quotient_ro wp) =
AlgWP?.reflect (quot_tree (reify (f ())))
false
AlgWP.fst
AlgWP.ignore_writes
val ignore_writes (#a: _) (#l: rwops{~(List.Tot.memP Write l)}) (#pre #post: _) (f: (unit -> AlgPP a (Write :: l) pre post)) : AlgPP a l pre (fun h0 x h1 -> h0 == h1)
val ignore_writes (#a: _) (#l: rwops{~(List.Tot.memP Write l)}) (#pre #post: _) (f: (unit -> AlgPP a (Write :: l) pre post)) : AlgPP a l pre (fun h0 x h1 -> h0 == h1)
let ignore_writes #a (#l:rwops{~(List.Tot.memP Write l)}) #pre #post (f : unit -> AlgPP a (Write::l) pre post) : AlgPP a l pre (fun h0 x h1 -> h0 == h1) = handle_into_ro #a #l #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 93, "end_line": 404, "start_col": 0, "start_line": 402 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x)) let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c let quot #a #wp (f : unit -> AlgWP a [Read] wp) : AlgWP a [Read] (quotient_ro wp) = AlgWP?.reflect (quot_tree (reify (f ()))) effect AlgPP (a:Type) (ll:rwops) (pre : state -> Type0) (post : state -> a -> state -> Type0) = AlgWP a ll (fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) let quotPP #a #pre #post (f : unit -> AlgPP a [Read] pre post) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1) = quot #_ #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f let null #a : st_wp a = fun s0 p -> forall r. p r let null_ro #a : st_wp a = quotient_ro null let null_ro1 #a : st_wp a = fun s0 p -> forall x. p (x, s0) let null_equiv_sanity a = assert (null_ro #a `equiv` null_ro1 #a) let bind_null_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires (null_ro `stronger` w) /\ (forall x. null_ro `stronger` f x)) (ensures null_ro `stronger` (bind_wp w f)) = () (* Similar to ro_tree_wp *) let rec null_ro_tree_wp #a (t : tree a [Read]) : Lemma (null_ro `stronger` (interp_as_wp t)) by (T.compute ()) // need this to trigger some unfoldings = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (null_ro `stronger` interp_as_wp (k x)) = null_ro_tree_wp (k x) in Classical.forall_intro aux; bind_null_ro read_wp (fun x -> interp_as_wp (k x)) (* Could this work automatically too? *) // ro_tree_wp (reify (f ())); // f () let __tree_handle_into_ro #a (#l:rwops{~(List.Tot.memP Write l)}) #wp (f : repr a (Write::l) wp) : repr a l null_ro = let f' : tree a (Write::l) = f in let h : tree a l = handle_tree f' (fun x -> Return x) (function Write -> fun i k -> k () | op -> fun i k -> Op op i k) in assert (sublist l [Read]); let h : tree a [Read] = h in null_ro_tree_wp h; h (* Take any computation, ignore its Writes, you get a read only WP *) let handle_into_ro #a (#l:rwops{~(List.Tot.memP Write l)}) #wp (f : unit -> AlgWP a (Write::l) wp) : AlgWP a l null_ro = AlgWP?.reflect (__tree_handle_into_ro (reify (f ())))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: Prims.unit -> AlgWP.AlgPP a) -> AlgWP.AlgPP a
AlgWP.AlgPP
[]
[]
[ "AlgWP.rwops", "Prims.l_not", "FStar.List.Tot.Base.memP", "Alg.op", "Alg.Write", "Alg.state", "Prims.unit", "Prims.Cons", "AlgWP.handle_into_ro", "FStar.Pervasives.Native.tuple2", "Prims.logical", "Prims.l_and", "Prims.l_Forall", "Prims.l_imp", "FStar.Pervasives.Native.Mktuple2", "Prims.eq2" ]
[]
false
true
false
false
false
let ignore_writes #a (#l: rwops{~(List.Tot.memP Write l)}) #pre #post (f: (unit -> AlgPP a (Write :: l) pre post)) : AlgPP a l pre (fun h0 x h1 -> h0 == h1) =
handle_into_ro #a #l #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f
false
AlgWP.fst
AlgWP.null
val null (#a: _) : st_wp a
val null (#a: _) : st_wp a
let null #a : st_wp a = fun s0 p -> forall r. p r
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 49, "end_line": 357, "start_col": 0, "start_line": 357 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x)) let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c let quot #a #wp (f : unit -> AlgWP a [Read] wp) : AlgWP a [Read] (quotient_ro wp) = AlgWP?.reflect (quot_tree (reify (f ()))) effect AlgPP (a:Type) (ll:rwops) (pre : state -> Type0) (post : state -> a -> state -> Type0) = AlgWP a ll (fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) let quotPP #a #pre #post (f : unit -> AlgPP a [Read] pre post) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1) = quot #_ #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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
AlgWP.st_wp a
Prims.Tot
[ "total" ]
[]
[ "Alg.state", "FStar.Pervasives.Native.tuple2", "Prims.l_Forall", "Prims.logical", "AlgWP.st_wp" ]
[]
false
false
false
true
false
let null #a : st_wp a =
fun s0 p -> forall r. p r
false
AlgWP.fst
AlgWP.handle_into_ro
val handle_into_ro (#a: _) (#l: rwops{~(List.Tot.memP Write l)}) (#wp: _) (f: (unit -> AlgWP a (Write :: l) wp)) : AlgWP a l null_ro
val handle_into_ro (#a: _) (#l: rwops{~(List.Tot.memP Write l)}) (#wp: _) (f: (unit -> AlgWP a (Write :: l) wp)) : AlgWP a l null_ro
let handle_into_ro #a (#l:rwops{~(List.Tot.memP Write l)}) #wp (f : unit -> AlgWP a (Write::l) wp) : AlgWP a l null_ro = AlgWP?.reflect (__tree_handle_into_ro (reify (f ())))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 57, "end_line": 400, "start_col": 0, "start_line": 398 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x)) let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c let quot #a #wp (f : unit -> AlgWP a [Read] wp) : AlgWP a [Read] (quotient_ro wp) = AlgWP?.reflect (quot_tree (reify (f ()))) effect AlgPP (a:Type) (ll:rwops) (pre : state -> Type0) (post : state -> a -> state -> Type0) = AlgWP a ll (fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) let quotPP #a #pre #post (f : unit -> AlgPP a [Read] pre post) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1) = quot #_ #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f let null #a : st_wp a = fun s0 p -> forall r. p r let null_ro #a : st_wp a = quotient_ro null let null_ro1 #a : st_wp a = fun s0 p -> forall x. p (x, s0) let null_equiv_sanity a = assert (null_ro #a `equiv` null_ro1 #a) let bind_null_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires (null_ro `stronger` w) /\ (forall x. null_ro `stronger` f x)) (ensures null_ro `stronger` (bind_wp w f)) = () (* Similar to ro_tree_wp *) let rec null_ro_tree_wp #a (t : tree a [Read]) : Lemma (null_ro `stronger` (interp_as_wp t)) by (T.compute ()) // need this to trigger some unfoldings = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (null_ro `stronger` interp_as_wp (k x)) = null_ro_tree_wp (k x) in Classical.forall_intro aux; bind_null_ro read_wp (fun x -> interp_as_wp (k x)) (* Could this work automatically too? *) // ro_tree_wp (reify (f ())); // f () let __tree_handle_into_ro #a (#l:rwops{~(List.Tot.memP Write l)}) #wp (f : repr a (Write::l) wp) : repr a l null_ro = let f' : tree a (Write::l) = f in let h : tree a l = handle_tree f' (fun x -> Return x) (function Write -> fun i k -> k () | op -> fun i k -> Op op i k) in assert (sublist l [Read]); let h : tree a [Read] = h in null_ro_tree_wp h; h
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: Prims.unit -> AlgWP.AlgWP a) -> AlgWP.AlgWP a
AlgWP.AlgWP
[]
[]
[ "AlgWP.rwops", "Prims.l_not", "FStar.List.Tot.Base.memP", "Alg.op", "Alg.Write", "AlgWP.st_wp", "Prims.unit", "Prims.Cons", "AlgWP.__tree_handle_into_ro", "AlgWP.null_ro" ]
[]
false
true
false
false
false
let handle_into_ro #a (#l: rwops{~(List.Tot.memP Write l)}) #wp (f: (unit -> AlgWP a (Write :: l) wp)) : AlgWP a l null_ro =
AlgWP?.reflect (__tree_handle_into_ro (reify (f ())))
false
FStar.Matrix.fst
FStar.Matrix.matrix_left_mul_identity_aux_2
val matrix_left_mul_identity_aux_2 (#c #eq #m: _) (add: CE.cm c eq) (mul: CE.cm c eq {is_absorber add.unit mul}) (mx: matrix c m m) (i j: under m) (k: nat{k = i + 1}) : Lemma (ensures (SP.foldm_snoc add (SB.init k (fun (k: under m) -> (ijth (matrix_mul_unit add mul m) i k) `mul.mult` (ijth mx k j))) ) `eq.eq` (ijth mx i j))
val matrix_left_mul_identity_aux_2 (#c #eq #m: _) (add: CE.cm c eq) (mul: CE.cm c eq {is_absorber add.unit mul}) (mx: matrix c m m) (i j: under m) (k: nat{k = i + 1}) : Lemma (ensures (SP.foldm_snoc add (SB.init k (fun (k: under m) -> (ijth (matrix_mul_unit add mul m) i k) `mul.mult` (ijth mx k j))) ) `eq.eq` (ijth mx i j))
let matrix_left_mul_identity_aux_2 #c #eq #m (add: CE.cm c eq) (mul: CE.cm c eq{is_absorber add.unit mul}) (mx: matrix c m m) (i j: under m) (k:nat{k=i+1}) : Lemma (ensures SP.foldm_snoc add (SB.init k (fun (k: under m) -> ijth (matrix_mul_unit add mul m) i k `mul.mult` ijth mx k j)) `eq.eq` ijth mx i j) = let unit = matrix_mul_unit add mul m in let mxu = matrix_mul add mul mx unit in let ( * ) = mul.mult in let ( $=$ ) = eq.eq in let gen (k: under m) = ijth unit i k * ijth mx k j in let full = SB.init k gen in let liat,last = SProp.un_snoc full in assert (k-1 <= i /\ k-1 >= 0); if (k-1)=0 then matrix_left_mul_identity_aux_0 add mul mx i j (k-1) else matrix_left_mul_identity_aux_1 add mul mx i j (k-1); matrix_mul_unit_ijth add mul m i (k-1); // This one reduces the rlimits needs to default SP.foldm_snoc_decomposition add full; liat_equals_init k gen; mul.identity (ijth mx i j); eq.reflexivity last; add.congruence last (SP.foldm_snoc add liat) last add.unit; add.identity last; add.commutativity last add.unit; mul.commutativity (ijth mx i j) mul.unit; eq.transitivity (add.mult last add.unit) (add.mult add.unit last) last; eq.transitivity (SP.foldm_snoc add full) (add.mult last add.unit) last; eq.transitivity last (mul.unit * ijth mx i j) (ijth mx i j); eq.transitivity (SP.foldm_snoc add full) last (ijth mx i j)
{ "file_name": "ulib/FStar.Matrix.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 980, "start_col": 0, "start_line": 950 }
(* Copyright 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. Author: A. Rozanov *) (* In this module we provide basic definitions to work with matrices via seqs, and define transpose transform together with theorems that assert matrix fold equality of original and transposed matrices. *) module FStar.Matrix module CE = FStar.Algebra.CommMonoid.Equiv module CF = FStar.Algebra.CommMonoid.Fold module SP = FStar.Seq.Permutation module SB = FStar.Seq.Base module SProp = FStar.Seq.Properties module ML = FStar.Math.Lemmas open FStar.IntegerIntervals open FStar.Mul open FStar.Seq.Equiv (* A little glossary that might help reading this file We don't list common terms like associativity and reflexivity. lhs, rhs left hand side, right hand side liat subsequence of all elements except the last (tail read backwards) snoc construction of sequence from a pair (liat, last) (cons read backwards) un_snoc decomposition of sequence into a pair (liat, last) foldm sum or product of all elements in a sequence using given CommMonoid foldm_snoc recursively defined sum/product of a sequence, starting from the last element congruence respect of equivalence ( = ) by a binary operation ( * ), a=b ==> a*x = b*x unit identity element (xu=x, ux=x) (not to be confused with invertible elements) *) type matrix c m n = z:SB.seq c { SB.length z = m*n } let seq_of_matrix #c #m #n mx = mx let ijth #c #m #n mx i j = SB.index mx (get_ij m n i j) let ijth_lemma #c #m #n mx i j : Lemma (ijth mx i j == SB.index (seq_of_matrix mx) (get_ij m n i j)) = () let matrix_of_seq #c m n s = s let foldm #c #eq #m #n cm mx = SP.foldm_snoc cm mx let matrix_fold_equals_fold_of_seq #c #eq #m #n cm mx : Lemma (ensures foldm cm mx `eq.eq` SP.foldm_snoc cm (seq_of_matrix mx)) [SMTPat(foldm cm mx)] = eq.reflexivity (foldm cm mx) let matrix_fold_internal #c #eq #m #n (cm:CE.cm c eq) (mx: matrix c m n) : Lemma (ensures foldm cm mx == SP.foldm_snoc cm mx) = () (* A flattened matrix (seq) constructed from generator function Notice how the domains of both indices are strictly controlled. *) let init #c (#m #n: pos) (generator: matrix_generator c m n) : matrix_of generator = let mn = m * n in let generator_ij ij = generator (get_i m n ij) (get_j m n ij) in let flat_indices = indices_seq mn in let result = SProp.map_seq generator_ij flat_indices in SProp.map_seq_len generator_ij flat_indices; assert (SB.length result == SB.length flat_indices); let aux (i: under m) (j: under n) : Lemma (SB.index (SProp.map_seq generator_ij flat_indices) (get_ij m n i j) == generator i j) = consistency_of_i_j m n i j; consistency_of_ij m n (get_ij m n i j); assert (generator_ij (get_ij m n i j) == generator i j); SProp.map_seq_index generator_ij flat_indices (get_ij m n i j) in let aux1 (ij: under mn) : Lemma (SB.index (SProp.map_seq generator_ij flat_indices) ij == generator_ij ij) = SProp.map_seq_index generator_ij flat_indices ij in FStar.Classical.forall_intro aux1; FStar.Classical.forall_intro_2 aux; result private let matrix_seq #c #m #n (gen: matrix_generator c m n) : (t:SB.seq c{ (SB.length t = (m*n)) /\ (forall (i: under m) (j: under n). SB.index t (get_ij m n i j) == gen i j) /\ (forall(ij: under (m*n)). SB.index t ij == gen (get_i m n ij) (get_j m n ij)) }) = init gen (* This auxiliary lemma establishes the decomposition of the seq-matrix into the concatenation of its first (m-1) rows and its last row (thus snoc) *) let matrix_append_snoc_lemma #c (#m #n: pos) (generator: matrix_generator c m n) : Lemma (matrix_seq generator == (SB.slice (matrix_seq generator) 0 ((m-1)*n)) `SB.append` (SB.slice (matrix_seq generator) ((m-1)*n) (m*n))) = SB.lemma_eq_elim (matrix_seq generator) (SB.append (SB.slice (matrix_seq generator) 0 ((m-1)*n)) (SB.slice (matrix_seq generator) ((m-1)*n) (m*n))) let matrix_seq_decomposition_lemma #c (#m:greater_than 1) (#n: pos) (generator: matrix_generator c m n) : Lemma ((matrix_seq generator) == SB.append (matrix_seq #c #(m-1) #n generator) (SB.slice (matrix_seq generator) ((m-1)*n) (m*n))) = SB.lemma_eq_elim (matrix_seq generator) ((matrix_seq #c #(m-1) #n generator) `SB.append` (SB.slice (matrix_seq generator) ((m-1)*n) (m*n))) (* This auxiliary lemma establishes the equality of the fold of the entire matrix to the op of folds of (the submatrix of the first (m-1) rows) and (the last row). *) let matrix_fold_snoc_lemma #c #eq (#m: not_less_than 2) (#n: pos) (cm: CE.cm c eq) (generator: matrix_generator c m n) : Lemma (assert ((m-1)*n < m*n); SP.foldm_snoc cm (matrix_seq generator) `eq.eq` cm.mult (SP.foldm_snoc cm (matrix_seq #c #(m-1) #n generator)) (SP.foldm_snoc cm (SB.slice (matrix_seq #c #m #n generator) ((m-1)*n) (m*n)))) = SB.lemma_eq_elim (matrix_seq generator) ((matrix_seq #c #(m-1) #n generator) `SB.append` (SB.slice (matrix_seq generator) ((m-1)*n) (m*n))); SP.foldm_snoc_append cm (matrix_seq #c #(m-1) #n generator) (SB.slice (matrix_seq generator) ((m-1)*n) (m*n)) (* There are many auxiliary lemmas like this that are extracted because lemma_eq_elim invocations often impact verification speed more than one might expect they would. *) let matrix_submatrix_lemma #c (#m: not_less_than 2) (#n: pos) (generator: matrix_generator c m n) : Lemma ((matrix_seq generator) == (matrix_seq (fun (i:under(m-1)) (j:under n) -> generator i j) `SB.append` SB.init n (generator (m-1)))) = SB.lemma_eq_elim (matrix_seq (fun (i:under (m-1)) (j:under n) -> generator i j)) (matrix_seq #c #(m-1) #n generator); SB.lemma_eq_elim (SB.slice (matrix_seq generator) ((m-1)*n) (m*n)) (SB.init n (generator (m-1))); matrix_seq_decomposition_lemma generator let matrix_seq_of_one_row_matrix #c #m #n (generator : matrix_generator c m n) : Lemma (requires m==1) (ensures matrix_seq generator == (SB.init n (generator 0))) = SB.lemma_eq_elim (matrix_seq generator) (SB.init n (generator 0)) let one_row_matrix_fold_aux #c #eq #m #n (cm:CE.cm c eq) (generator : matrix_generator c m n) : Lemma (requires m=1) (ensures foldm cm (init generator) `eq.eq` SP.foldm_snoc cm (SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (generator i)))) /\ SP.foldm_snoc cm (seq_of_matrix (init generator)) `eq.eq` SP.foldm_snoc cm (SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (generator i))))) = let lhs_seq = matrix_seq generator in let rhs_seq = SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (generator i))) in let lhs = SP.foldm_snoc cm (matrix_seq generator) in let rhs = SP.foldm_snoc cm rhs_seq in SP.foldm_snoc_singleton cm (SP.foldm_snoc cm (SB.init n (generator 0))); SB.lemma_eq_elim (SB.create 1 (SP.foldm_snoc cm (SB.init n (generator 0)))) (SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (generator i)))); matrix_seq_of_one_row_matrix generator; eq.symmetry rhs lhs let fold_of_subgen_aux #c #eq (#m:pos{m>1}) #n (cm: CE.cm c eq) (gen: matrix_generator c m n) (subgen: matrix_generator c (m-1) n) : Lemma (requires subgen == (fun (i: under (m-1)) (j: under n) -> gen i j)) (ensures forall (i: under (m-1)). SP.foldm_snoc cm (SB.init n (subgen i)) == SP.foldm_snoc cm (SB.init n (gen i))) = let aux_pat (i: under (m-1)) : Lemma (SP.foldm_snoc cm (SB.init n (subgen i)) == SP.foldm_snoc cm (SB.init n (gen i))) = SB.lemma_eq_elim (SB.init n (subgen i)) (SB.init n (gen i)) in Classical.forall_intro aux_pat let arithm_aux (m: pos{m>1}) (n: pos) : Lemma ((m-1)*n < m*n) = () let terminal_case_aux #c #eq (#p:pos{p=1}) #n (cm:CE.cm c eq) (generator: matrix_generator c p n) (m: pos{m<=p}) : Lemma (ensures SP.foldm_snoc cm (SB.slice (seq_of_matrix (init generator)) 0 (m*n)) `eq.eq` SP.foldm_snoc cm (SB.init m (fun (i:under m) -> SP.foldm_snoc cm (SB.init n (generator i))))) = one_row_matrix_fold_aux cm generator #push-options "--ifuel 0 --fuel 1 --z3rlimit 10" let terminal_case_two_aux #c #eq (#p:pos) #n (cm:CE.cm c eq) (generator: matrix_generator c p n) (m: pos{m=1}) : Lemma (ensures SP.foldm_snoc cm (SB.slice (seq_of_matrix (init generator)) 0 (m*n)) `eq.eq` SP.foldm_snoc cm (SB.init m (fun (i:under m) -> SP.foldm_snoc cm (SB.init n (generator i))))) = SP.foldm_snoc_singleton cm (SP.foldm_snoc cm (SB.init n (generator 0))); assert (SP.foldm_snoc cm (SB.init m (fun (i:under m) -> SP.foldm_snoc cm (SB.init n (generator i)))) `eq.eq` SP.foldm_snoc cm (SB.init n (generator 0))); let line = SB.init n (generator 0) in let slice = SB.slice (matrix_seq generator) 0 n in let aux (ij: under n) : Lemma (SB.index slice ij == SB.index line ij) = Math.Lemmas.small_div ij n; Math.Lemmas.small_mod ij n in Classical.forall_intro aux; SB.lemma_eq_elim line slice; eq.symmetry (SP.foldm_snoc cm (SB.init m (fun (i:under m) -> SP.foldm_snoc cm (SB.init n (generator i))))) (SP.foldm_snoc cm line) #pop-options let liat_equals_init #c (m:pos) (gen: under m -> c) : Lemma (fst (SProp.un_snoc (SB.init m gen)) == SB.init (m-1) gen) = SB.lemma_eq_elim (fst (SProp.un_snoc (SB.init m gen))) (SB.init (m-1) gen) let math_aux (m n: pos) (j: under n) : Lemma (j+((m-1)*n) < m*n) = () let math_aux_2 (m n: pos) (j: under n) : Lemma (get_j m n (j+(m-1)*n) == j) = Math.Lemmas.modulo_addition_lemma j n (m-1); Math.Lemmas.small_mod j n let math_aux_3 (m n: pos) (j: under n) : Lemma (get_i m n (j+(m-1)*n) == (m-1)) = Math.Lemmas.division_addition_lemma j n (m-1); Math.Lemmas.small_div j n let math_aux_4 (m n: pos) (j: under n) : Lemma ((j+((m-1)*n)) - ((m-1)*n) == j) = () let seq_eq_from_member_eq #c (n: pos) (p q: (z:SB.seq c{SB.length z=n})) (proof: (i: under n) -> Lemma (SB.index p i == SB.index q i)) : Lemma (p == q) = Classical.forall_intro proof; SB.lemma_eq_elim p q let math_wut_lemma (x: pos) : Lemma (requires x>1) (ensures x-1 > 0) = () (* This proof used to be very unstable, so I rewrote it with as much precision and control over lambdas as possible. I also left intact some trivial auxiliaries and the quake option in order to catch regressions the moment they happen instead of several releases later -- Alex *) #push-options "--ifuel 0 --fuel 0 --z3rlimit 15" #restart-solver let rec matrix_fold_equals_double_fold #c #eq (#p:pos) #n (cm:CE.cm c eq) (generator: matrix_generator c p n) (m: pos{m<=p}) : Lemma (ensures SP.foldm_snoc cm (SB.slice (seq_of_matrix (init generator)) 0 (m*n)) `eq.eq` SP.foldm_snoc cm (SB.init m (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (generator i))))) (decreases m) = if p=1 then terminal_case_aux cm generator m else if m=1 then terminal_case_two_aux cm generator m else let lhs_seq = (SB.slice (matrix_seq generator) 0 (m*n)) in let rhs_seq_gen = fun (i: under m) -> SP.foldm_snoc cm (SB.init n (generator i)) in let rhs_seq_subgen = fun (i: under (m-1)) -> SP.foldm_snoc cm (SB.init n (generator i)) in let rhs_seq = SB.init m rhs_seq_gen in let lhs = SP.foldm_snoc cm lhs_seq in let rhs = SP.foldm_snoc cm rhs_seq in let matrix = lhs_seq in let submatrix = SB.slice (matrix_seq generator) 0 ((m-1)*n) in let last_row = SB.slice (matrix_seq generator) ((m-1)*n) (m*n) in SB.lemma_len_slice (matrix_seq generator) ((m-1)*n) (m*n); assert (SB.length last_row = n); SB.lemma_eq_elim matrix (SB.append submatrix last_row); SP.foldm_snoc_append cm submatrix last_row; matrix_fold_equals_double_fold #c #eq #p #n cm generator (m-1); SB.lemma_eq_elim (SB.init (m-1) rhs_seq_gen) (SB.init (m-1) rhs_seq_subgen); let aux (j: under n) : Lemma (SB.index last_row j == generator (m-1) j) = SB.lemma_index_app2 submatrix last_row (j+((m-1)*n)); math_aux_2 m n j; math_aux_3 m n j; math_aux_4 m n j; () in Classical.forall_intro aux; let rhs_liat, rhs_last = SProp.un_snoc rhs_seq in let rhs_last_seq = SB.init n (generator (m-1)) in liat_equals_init m rhs_seq_gen; SP.foldm_snoc_decomposition cm rhs_seq; let aux_2 (j: under n) : Lemma (SB.index last_row j == SB.index rhs_last_seq j) = () in seq_eq_from_member_eq n last_row rhs_last_seq aux_2; SB.lemma_eq_elim rhs_liat (SB.init (m-1) rhs_seq_gen); cm.commutativity (SP.foldm_snoc cm submatrix) (SP.foldm_snoc cm last_row); eq.transitivity lhs (SP.foldm_snoc cm submatrix `cm.mult` SP.foldm_snoc cm last_row) (SP.foldm_snoc cm last_row `cm.mult` SP.foldm_snoc cm submatrix); eq.reflexivity (SP.foldm_snoc cm last_row); cm.congruence (SP.foldm_snoc cm last_row) (SP.foldm_snoc cm submatrix) (SP.foldm_snoc cm last_row) (SP.foldm_snoc cm (SB.init (m-1) rhs_seq_subgen)); eq.transitivity lhs (SP.foldm_snoc cm last_row `cm.mult` SP.foldm_snoc cm submatrix) rhs #pop-options let matrix_fold_equals_fold_of_seq_folds #c #eq #m #n cm generator : Lemma (ensures foldm cm (init generator) `eq.eq` SP.foldm_snoc cm (SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (generator i)))) /\ SP.foldm_snoc cm (seq_of_matrix (init generator)) `eq.eq` SP.foldm_snoc cm (SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (generator i))))) = matrix_fold_equals_double_fold cm generator m; assert ((SB.slice (seq_of_matrix (init generator)) 0 (m*n)) == seq_of_matrix (init generator)); SB.lemma_eq_elim (SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (generator i)))) (SB.init m (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (generator i)))); assert ((SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (generator i)))) == (SB.init m (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (generator i))))); () (* This auxiliary lemma shows that the fold of the last line of a matrix is equal to the corresponding fold of the generator function *) let matrix_last_line_equals_gen_fold #c #eq (#m #n: pos) (cm: CE.cm c eq) (generator: matrix_generator c m n) : Lemma (SP.foldm_snoc cm (SB.slice (matrix_seq generator) ((m-1)*n) (m*n)) `eq.eq` CF.fold cm 0 (n-1) (generator (m-1))) = let slice = SB.slice #c in let foldm_snoc = SP.foldm_snoc #c #eq in assert (matrix_seq generator == seq_of_matrix (init generator)); let init = SB.init #c in let lemma_eq_elim = SB.lemma_eq_elim #c in lemma_eq_elim (slice (matrix_seq generator) ((m-1)*n) (m*n)) (init n (generator (m-1))); let g : ifrom_ito 0 (n-1) -> c = generator (m-1) in CF.fold_equals_seq_foldm cm 0 (n-1) g; let gen = CF.init_func_from_expr g 0 (n-1) in eq.reflexivity (foldm_snoc cm (init (closed_interval_size 0 (n-1)) gen)); lemma_eq_elim (slice (matrix_seq generator) ((m-1)*n) (m*n)) (init (closed_interval_size 0 (n-1)) gen); eq.symmetry (CF.fold cm 0 (n-1) (generator (m-1))) (foldm_snoc cm (init (closed_interval_size 0 (n-1)) gen)); eq.transitivity (foldm_snoc cm (slice (matrix_seq generator) ((m-1)*n) (m*n))) (foldm_snoc cm (init (closed_interval_size 0 (n-1)) gen)) (CF.fold cm 0 (n-1) (generator (m-1))) (* This lemma proves that a matrix fold is the same thing as double-fold of its generator function against full indices ranges *) #push-options "--ifuel 0 --fuel 0" let rec matrix_fold_aux #c #eq // lemma needed for precise generator domain control (#gen_m #gen_n: pos) // full generator domain (cm: CE.cm c eq) (m: ifrom_ito 1 gen_m) (n: ifrom_ito 1 gen_n) //subdomain (generator: matrix_generator c gen_m gen_n) : Lemma (ensures SP.foldm_snoc cm (matrix_seq #c #m #n generator) `eq.eq` CF.fold cm 0 (m-1) (fun (i: under m) -> CF.fold cm 0 (n-1) (generator i))) (decreases m) = Classical.forall_intro_2 (ijth_lemma (init generator)); let slice = SB.slice #c in let foldm_snoc = SP.foldm_snoc #c #eq in let lemma_eq_elim = SB.lemma_eq_elim #c in if m = 1 then begin matrix_fold_equals_fold_of_seq cm (init generator); matrix_last_line_equals_gen_fold #c #eq #m #n cm generator; CF.fold_singleton_lemma cm 0 (fun (i:under m) -> CF.fold cm 0 (n-1) (generator i)); assert (CF.fold cm 0 (m-1) (fun (i: under m) -> CF.fold cm 0 (n-1) (generator i)) == CF.fold cm 0 (n-1) (generator 0)) end else begin Classical.forall_intro_3 (Classical.move_requires_3 eq.transitivity); matrix_fold_aux cm (m-1) n generator; let outer_func (i: under m) = CF.fold cm 0 (n-1) (generator i) in let outer_func_on_subdomain (i: under (m-1)) = CF.fold cm 0 (n-1) (generator i) in CF.fold_equality cm 0 (m-2) outer_func_on_subdomain outer_func; CF.fold_snoc_decomposition cm 0 (m-1) outer_func; matrix_fold_snoc_lemma #c #eq #m #n cm generator; matrix_last_line_equals_gen_fold #c #eq #m #n cm generator; cm.congruence (foldm_snoc cm (matrix_seq #c #(m-1) #n generator)) (foldm_snoc cm (slice (matrix_seq #c #m #n generator) ((m-1)*n) (m*n))) (CF.fold cm 0 (m-2) outer_func) (CF.fold cm 0 (n-1) (generator (m-1))) end #pop-options (* This lemma establishes that the fold of a matrix is equal to nested Algebra.CommMonoid.Fold.fold over the matrix generator *) let matrix_fold_equals_func_double_fold #c #eq #m #n cm generator : Lemma (foldm cm (init generator) `eq.eq` CF.fold cm 0 (m-1) (fun (i:under m) -> CF.fold cm 0 (n-1) (generator i))) = matrix_fold_aux cm m n generator (* This function provides the transposed matrix generator, with indices swapped Notice how the forall property of the result function is happily proved automatically by z3 :) *) let transposed_matrix_gen #c #m #n (generator: matrix_generator c m n) : (f: matrix_generator c n m { forall i j. f j i == generator i j }) = fun j i -> generator i j (* This lemma shows that the transposed matrix is a permutation of the original one *) let matrix_transpose_is_permutation #c #m #n generator : Lemma (SP.is_permutation (seq_of_matrix (init generator)) (seq_of_matrix (init (transposed_matrix_gen generator))) (transpose_ji m n)) = let matrix_transposed_eq_lemma #c (#m #n: pos) (gen: matrix_generator c m n) (ij: under (m*n)) : Lemma (SB.index (seq_of_matrix (init gen)) ij == SB.index (seq_of_matrix (init (transposed_matrix_gen gen))) (transpose_ji m n ij)) = ijth_lemma (init gen) (get_i m n ij) (get_j m n ij); ijth_lemma (init (transposed_matrix_gen gen)) (get_i n m (transpose_ji m n ij)) (get_j n m (transpose_ji m n ij)); () in let transpose_inequality_lemma (m n: pos) (ij: under (m*n)) (kl: under (n*m)) : Lemma (requires kl <> ij) (ensures transpose_ji m n ij <> transpose_ji m n kl) = dual_indices m n ij; dual_indices m n kl in Classical.forall_intro (matrix_transposed_eq_lemma generator); Classical.forall_intro_2 (Classical.move_requires_2 (transpose_inequality_lemma m n)); SP.reveal_is_permutation (seq_of_matrix (init generator)) (seq_of_matrix (init (transposed_matrix_gen generator))) (transpose_ji m n) (* Fold over matrix equals fold over transposed matrix *) let matrix_fold_equals_fold_of_transpose #c #eq #m #n (cm: CE.cm c eq) (gen: matrix_generator c m n) : Lemma (foldm cm (init gen) `eq.eq` foldm cm (init (transposed_matrix_gen gen))) = let matrix_seq #c #m #n (g: matrix_generator c m n) = (seq_of_matrix (init g)) in let matrix_mn = matrix_seq gen in let matrix_nm = matrix_seq (transposed_matrix_gen gen) in matrix_transpose_is_permutation gen; SP.foldm_snoc_perm cm (matrix_seq gen) (matrix_seq (transposed_matrix_gen gen)) (transpose_ji m n); matrix_fold_equals_fold_of_seq cm (init gen); matrix_fold_equals_fold_of_seq cm (init (transposed_matrix_gen gen)); eq.symmetry (foldm cm (init (transposed_matrix_gen gen))) (SP.foldm_snoc cm (matrix_seq (transposed_matrix_gen gen))); eq.transitivity (foldm cm (init gen)) (SP.foldm_snoc cm (matrix_seq gen)) (SP.foldm_snoc cm (matrix_seq (transposed_matrix_gen gen))); eq.transitivity (foldm cm (init gen)) (SP.foldm_snoc cm (matrix_seq (transposed_matrix_gen gen))) (foldm cm (init (transposed_matrix_gen gen))) let matrix_eq_fun #c (#m #n: pos) (eq: CE.equiv c) (ma mb: matrix c m n) = eq_of_seq eq (seq_of_matrix ma) (seq_of_matrix mb) (* Matrix equivalence, defined as element-wise equivalence of its underlying flattened sequence, is constructed trivially from the element equivalence and the lemmas defined above. *) let matrix_equiv #c (eq: CE.equiv c) (m n: pos) : CE.equiv (matrix c m n) = CE.EQ (matrix_eq_fun eq) (fun m -> eq_of_seq_reflexivity eq (seq_of_matrix m)) (fun ma mb -> eq_of_seq_symmetry eq (seq_of_matrix ma) (seq_of_matrix mb)) (fun ma mb mc -> eq_of_seq_transitivity eq (seq_of_matrix ma) (seq_of_matrix mb) (seq_of_matrix mc)) (* Equivalence of matrices means equivalence of all corresponding elements *) let matrix_equiv_ijth #c (#m #n: pos) (eq: CE.equiv c) (ma mb: matrix c m n) (i: under m) (j: under n) : Lemma (requires (matrix_equiv eq m n).eq ma mb) (ensures ijth ma i j `eq.eq` ijth mb i j) = eq_of_seq_element_equality eq (seq_of_matrix ma) (seq_of_matrix mb) (* Equivalence of all corresponding elements means equivalence of matrices *) let matrix_equiv_from_element_eq #c (#m #n: pos) (eq: CE.equiv c) (ma mb: matrix c m n) : Lemma (requires (forall (i: under m) (j: under n). ijth ma i j `eq.eq` ijth mb i j)) (ensures matrix_eq_fun eq ma mb) = assert (SB.length (seq_of_matrix ma) = SB.length (seq_of_matrix mb)); let s1 = seq_of_matrix ma in let s2 = seq_of_matrix mb in assert (forall (ij: under (m*n)). SB.index s1 ij == ijth ma (get_i m n ij) (get_j m n ij)); assert (forall (ij: under (m*n)). SB.index s2 ij == ijth mb (get_i m n ij) (get_j m n ij)); assert (forall (ij: under (m*n)). SB.index s1 ij `eq.eq` SB.index s2 ij); eq_of_seq_from_element_equality eq (seq_of_matrix ma) (seq_of_matrix mb) (* We construct addition CommMonoid from the following definitions *) let matrix_add_is_associative #c #eq #m #n (add: CE.cm c eq) (ma mb mc: matrix c m n) : Lemma (matrix_add add (matrix_add add ma mb) mc `(matrix_equiv eq m n).eq` matrix_add add ma (matrix_add add mb mc)) = matrix_equiv_from_proof eq (matrix_add add (matrix_add add ma mb) mc) (matrix_add add ma (matrix_add add mb mc)) (fun i j -> add.associativity (ijth ma i j) (ijth mb i j) (ijth mc i j)) let matrix_add_is_commutative #c #eq (#m #n: pos) (add: CE.cm c eq) (ma mb: matrix c m n) : Lemma (matrix_add add ma mb `(matrix_equiv eq m n).eq` matrix_add add mb ma) = matrix_equiv_from_proof eq (matrix_add add ma mb) (matrix_add add mb ma) (fun i j -> add.commutativity (ijth ma i j) (ijth mb i j)) let matrix_add_congruence #c #eq (#m #n: pos) (add: CE.cm c eq) (ma mb mc md: matrix c m n) : Lemma (requires matrix_eq_fun eq ma mc /\ matrix_eq_fun eq mb md) (ensures matrix_add add ma mb `matrix_eq_fun eq` matrix_add add mc md) = matrix_equiv_from_proof eq (matrix_add add ma mb) (matrix_add add mc md) (fun i j -> matrix_equiv_ijth eq ma mc i j; matrix_equiv_ijth eq mb md i j; add.congruence (ijth ma i j) (ijth mb i j) (ijth mc i j) (ijth md i j)) let matrix_add_zero #c #eq (add: CE.cm c eq) (m n: pos) : (z: matrix c m n { forall (i: under m) (j: under n). ijth z i j == add.unit }) = matrix_of_seq m n (SB.create (m*n) add.unit) let matrix_add_identity #c #eq (add: CE.cm c eq) (#m #n: pos) (mx: matrix c m n) : Lemma (matrix_add add (matrix_add_zero add m n) mx `matrix_eq_fun eq` mx) = matrix_equiv_from_proof eq (matrix_add add (matrix_add_zero add m n) mx) mx (fun i j -> add.identity (ijth mx i j)) let matrix_add_comm_monoid #c #eq (add: CE.cm c eq) (m n: pos) : CE.cm (matrix c m n) (matrix_equiv eq m n) = CE.CM (matrix_add_zero add m n) (matrix_add add) (matrix_add_identity add) (matrix_add_is_associative add) (matrix_add_is_commutative add) (matrix_add_congruence add) (* equivalence of addressing styles *) let matrix_row_col_lemma #c #m #n (mx: matrix c m n) (i: under m) (j: under n) : Lemma (ijth mx i j == SB.index (row mx i) j /\ ijth mx i j == SB.index (col mx j) i) = () (* See how lemma_eq_elim is defined, note the SMTPat there. Invoking this is often more efficient in big proofs than invoking lemma_eq_elim directly. *) let seq_of_products_lemma #c #eq (mul: CE.cm c eq) (s: SB.seq c) (t: SB.seq c {SB.length t == SB.length s}) (r: SB.seq c{SB.equal r (SB.init (SB.length s) (fun (i: under (SB.length s)) -> SB.index s i `mul.mult` SB.index t i))}) : Lemma (seq_of_products mul s t == r) = () let dot_lemma #c #eq add mul s t : Lemma (dot add mul s t == SP.foldm_snoc add (seq_of_products mul s t)) = () let matrix_mul_gen #c #eq #m #n #p (add mul: CE.cm c eq) (mx: matrix c m n) (my: matrix c n p) (i: under m) (k: under p) = dot add mul (row mx i) (col my k) let matrix_mul #c #eq #m #n #p (add mul: CE.cm c eq) (mx: matrix c m n) (my: matrix c n p) = init (matrix_mul_gen add mul mx my) (* the following lemmas improve verification performance. *) (* Sometimes this fact gets lost and needs an explicit proof *) let seq_last_index #c (s: SB.seq c{SB.length s > 0}) : Lemma (SProp.last s == SB.index s (SB.length s - 1)) = () (* It often takes assert_norm to obtain the fact that, (fold s == last s `op` fold (slice s 0 (length s - 1))). Invoking this lemma instead offers a more stable option. *) let seq_fold_decomposition #c #eq (cm: CE.cm c eq) (s: SB.seq c{SB.length s > 0}) : Lemma (SP.foldm_snoc cm s == cm.mult (SProp.last s) (SP.foldm_snoc cm (fst (SProp.un_snoc s)))) = () (* Using common notation for algebraic operations instead of `mul` / `add` infix simplifies the code and makes it more compact. *) let rec foldm_snoc_distributivity_left #c #eq (mul add: CE.cm c eq) (a: c) (s: SB.seq c) : Lemma (requires is_fully_distributive mul add /\ is_absorber add.unit mul) (ensures mul.mult a (SP.foldm_snoc add s) `eq.eq` SP.foldm_snoc add (const_op_seq mul a s)) (decreases SB.length s) = if SB.length s > 0 then let ((+), ( * ), (=)) = add.mult, mul.mult, eq.eq in let sum s = SP.foldm_snoc add s in let liat, last = SProp.un_snoc s in let rhs_liat, rhs_last = SProp.un_snoc (const_op_seq mul a s) in foldm_snoc_distributivity_left mul add a liat; SB.lemma_eq_elim rhs_liat (const_op_seq mul a liat); eq.reflexivity rhs_last; add.congruence rhs_last (a*sum liat) rhs_last (sum rhs_liat); eq.transitivity (a*sum s) (rhs_last + a*sum liat) (rhs_last + sum rhs_liat) let rec foldm_snoc_distributivity_right #c #eq (mul add: CE.cm c eq) (s: SB.seq c) (a: c) : Lemma (requires is_fully_distributive mul add /\ is_absorber add.unit mul) (ensures mul.mult (SP.foldm_snoc add s) a `eq.eq` SP.foldm_snoc add (seq_op_const mul s a)) (decreases SB.length s) = if SB.length s > 0 then let ((+), ( * ), (=)) = add.mult, mul.mult, eq.eq in let sum s = SP.foldm_snoc add s in let liat, last = SProp.un_snoc s in let rhs_liat, rhs_last = SProp.un_snoc (seq_op_const mul s a) in foldm_snoc_distributivity_right mul add liat a; SB.lemma_eq_elim rhs_liat (seq_op_const mul liat a); eq.reflexivity rhs_last; add.congruence rhs_last (sum liat*a) rhs_last (sum rhs_liat); eq.transitivity (sum s*a) (rhs_last + sum liat*a) (rhs_last + sum rhs_liat) let foldm_snoc_distributivity_right_eq #c #eq (mul add: CE.cm c eq) (s: SB.seq c) (a: c) (r: SB.seq c) : Lemma (requires is_fully_distributive mul add /\ is_absorber add.unit mul /\ SB.equal r (seq_op_const mul s a)) (ensures mul.mult (SP.foldm_snoc add s) a `eq.eq` SP.foldm_snoc add r) = foldm_snoc_distributivity_right mul add s a let foldm_snoc_distributivity_left_eq #c #eq (mul add: CE.cm c eq) (a: c) (s: SB.seq c) (r: SB.seq c{SB.equal r (const_op_seq mul a s)}) : Lemma (requires is_fully_distributive mul add /\ is_absorber add.unit mul) (ensures (mul.mult a(SP.foldm_snoc add s)) `eq.eq` SP.foldm_snoc add r) = foldm_snoc_distributivity_left mul add a s let matrix_mul_ijth #c #eq #m #n #k (add mul: CE.cm c eq) (mx: matrix c m n) (my: matrix c n k) i h : Lemma (ijth (matrix_mul add mul mx my) i h == dot add mul (row mx i) (col my h)) = () let matrix_mul_ijth_as_sum #c #eq #m #n #p (add mul: CE.cm c eq) (mx: matrix c m n) (my: matrix c n p) i k : Lemma (ijth (matrix_mul add mul mx my) i k == SP.foldm_snoc add (SB.init n (fun (j: under n) -> mul.mult (ijth mx i j) (ijth my j k)))) = let r = SB.init n (fun (j: under n) -> mul.mult (ijth mx i j) (ijth my j k)) in assert (ijth (matrix_mul add mul mx my) i k == SP.foldm_snoc add (seq_of_products mul (row mx i) (col my k))); seq_of_products_lemma mul (row mx i) (col my k) r let matrix_mul_ijth_eq_sum_of_seq #c #eq #m #n #p (add: CE.cm c eq) (mul: CE.cm c eq{is_fully_distributive mul add /\ is_absorber add.unit mul}) (mx: matrix c m n) (my: matrix c n p) (i: under m) (k: under p) (r: SB.seq c{r `SB.equal` seq_of_products mul (row mx i) (col my k)}) : Lemma (ijth (matrix_mul add mul mx my) i k == SP.foldm_snoc add r) = () let double_foldm_snoc_transpose_lemma #c #eq (#m #n: pos) (cm: CE.cm c eq) (f: under m -> under n -> c) : Lemma (SP.foldm_snoc cm (SB.init m (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (fun (j: under n) -> f i j)))) `eq.eq` SP.foldm_snoc cm (SB.init n (fun (j: under n) -> SP.foldm_snoc cm (SB.init m (fun (i: under m) -> f i j))))) = Classical.forall_intro_2 (Classical.move_requires_2 eq.symmetry); let gen : matrix_generator c m n = f in let mx = init gen in let mx_seq = matrix_seq gen in matrix_fold_equals_fold_of_seq_folds cm gen; let aux (i: under m) : Lemma (SB.init n (gen i) == SB.init n (fun (j: under n) -> f i j)) = SB.lemma_eq_elim (SB.init n (gen i))(SB.init n (fun (j: under n) -> f i j)) in Classical.forall_intro aux; SB.lemma_eq_elim (SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (gen i)))) (SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (fun (j: under n) -> f i j)))); SB.lemma_eq_elim (SB.init m (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (fun (j: under n) -> f i j)))) (SB.init m (fun i -> SP.foldm_snoc cm (SB.init n (fun (j: under n) -> f i j)))); matrix_transpose_is_permutation gen; matrix_fold_equals_fold_of_transpose cm gen; let trans_gen = transposed_matrix_gen gen in let mx_trans = init trans_gen in let mx_trans_seq = matrix_seq trans_gen in matrix_fold_equals_fold_of_seq_folds cm trans_gen; assert (foldm cm mx_trans `eq.eq` SP.foldm_snoc cm (SB.init n (fun j -> SP.foldm_snoc cm (SB.init m (trans_gen j))))); let aux_tr_lemma (j: under n) : Lemma ((SB.init m (trans_gen j)) == (SB.init m (fun (i: under m) -> f i j))) = SB.lemma_eq_elim (SB.init m (trans_gen j)) (SB.init m (fun (i: under m) -> f i j)) in Classical.forall_intro aux_tr_lemma; SB.lemma_eq_elim (SB.init n (fun j -> SP.foldm_snoc cm (SB.init m (trans_gen j)))) (SB.init n (fun (j:under n) -> SP.foldm_snoc cm (SB.init m (fun (i: under m) -> f i j)))); assert (foldm cm mx_trans `eq.eq` SP.foldm_snoc cm (SB.init n (fun (j:under n) -> SP.foldm_snoc cm (SB.init m (fun (i: under m) -> f i j))))); eq.transitivity (SP.foldm_snoc cm (SB.init m (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (fun (j: under n) -> f i j))))) (foldm cm mx) (foldm cm mx_trans); eq.transitivity (SP.foldm_snoc cm (SB.init m (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (fun (j: under n) -> f i j))))) (foldm cm mx_trans) (SP.foldm_snoc cm (SB.init n (fun (j:under n) -> SP.foldm_snoc cm (SB.init m (fun (i: under m) -> f i j))))) let matrix_mul_ijth_eq_sum_of_seq_for_init #c #eq #m #n #p (add mul: CE.cm c eq) (mx: matrix c m n) (my: matrix c n p) i k (f: under n -> c { SB.init n f `SB.equal` seq_of_products mul (row mx i) (col my k)}) : Lemma (ijth (matrix_mul add mul mx my) i k == SP.foldm_snoc add (SB.init n f)) = () let double_foldm_snoc_of_equal_generators #c #eq (#m #n: pos) (cm: CE.cm c eq) (f g: under m -> under n -> c) : Lemma (requires (forall (i: under m) (j: under n). f i j `eq.eq` g i j)) (ensures SP.foldm_snoc cm (SB.init m (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (fun (j: under n) -> f i j)))) `eq.eq` SP.foldm_snoc cm (SB.init m (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (fun (j: under n) -> g i j))))) = let aux i : Lemma (SP.foldm_snoc cm (SB.init n (fun (j: under n) -> f i j)) `eq.eq` SP.foldm_snoc cm (SB.init n (fun (j: under n) -> g i j))) = SP.foldm_snoc_of_equal_inits cm (fun j -> f i j) (fun j -> g i j) in Classical.forall_intro aux; SP.foldm_snoc_of_equal_inits cm (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (fun (j: under n) -> f i j))) (fun (i: under m) -> SP.foldm_snoc cm (SB.init n (fun (j: under n) -> g i j))) #push-options "--z3rlimit 15 --ifuel 0 --fuel 0" let matrix_mul_is_associative #c #eq #m #n #p #q (add: CE.cm c eq) (mul: CE.cm c eq{is_fully_distributive mul add /\ is_absorber add.unit mul}) (mx: matrix c m n) (my: matrix c n p) (mz: matrix c p q) : Lemma (matrix_eq_fun eq ((matrix_mul add mul mx my) `matrix_mul add mul` mz) (matrix_mul add mul mx (matrix_mul add mul my mz))) = let rhs = mx `matrix_mul add mul` (my `matrix_mul add mul` mz) in let lhs = (mx `matrix_mul add mul` my) `matrix_mul add mul` mz in let mxy = matrix_mul add mul mx my in let myz = matrix_mul add mul my mz in let ((+), ( * ), (=)) = add.mult, mul.mult, eq.eq in let aux i l : squash (ijth lhs i l = ijth rhs i l) = let sum_j (f: under n -> c) = SP.foldm_snoc add (SB.init n f) in let sum_k (f: under p -> c) = SP.foldm_snoc add (SB.init p f) in let xy_products_init k j = ijth mx i j * ijth my j k in let xy_cell_as_sum k = sum_j (xy_products_init k) in let xy_cell_lemma k : Lemma (ijth mxy i k == xy_cell_as_sum k) = matrix_mul_ijth_eq_sum_of_seq_for_init add mul mx my i k (xy_products_init k) in Classical.forall_intro xy_cell_lemma; let xy_z_products_init k = xy_cell_as_sum k * ijth mz k l in matrix_mul_ijth_eq_sum_of_seq_for_init add mul mxy mz i l xy_z_products_init; let full_init_kj k j = (ijth mx i j * ijth my j k) * ijth mz k l in let full_init_jk j k = (ijth mx i j * ijth my j k) * ijth mz k l in let full_init_rh j k = ijth mx i j * (ijth my j k * ijth mz k l) in let sum_jk (f: (under n -> under p -> c)) = sum_j (fun j -> sum_k (fun k -> f j k)) in let sum_kj (f: (under p -> under n -> c)) = sum_k (fun k -> sum_j (fun j -> f k j)) in let xy_z_distr k : Lemma (((xy_cell_as_sum k) * (ijth mz k l)) = sum_j (full_init_kj k)) = foldm_snoc_distributivity_right_eq mul add (SB.init n (xy_products_init k)) (ijth mz k l) (SB.init n (full_init_kj k)) in Classical.forall_intro xy_z_distr; SP.foldm_snoc_of_equal_inits add xy_z_products_init (fun k -> sum_j (full_init_kj k)); double_foldm_snoc_transpose_lemma add full_init_kj; eq.transitivity (ijth lhs i l) (sum_kj full_init_kj) (sum_jk full_init_jk); let aux_rh j k : Lemma (full_init_jk j k = full_init_rh j k) = mul.associativity (ijth mx i j) (ijth my j k) (ijth mz k l) in Classical.forall_intro_2 aux_rh; double_foldm_snoc_of_equal_generators add full_init_jk full_init_rh; eq.transitivity (ijth lhs i l) (sum_jk full_init_jk) (sum_jk full_init_rh); // now expand the right hand side, fully dual to the first part of the lemma. let yz_products_init j k = ijth my j k * ijth mz k l in let yz_cell_as_sum j = sum_k (yz_products_init j) in let x_yz_products_init j = ijth mx i j * yz_cell_as_sum j in let yz_cell_lemma j : Lemma (ijth myz j l == sum_k (yz_products_init j)) = matrix_mul_ijth_eq_sum_of_seq_for_init add mul my mz j l (yz_products_init j); () in Classical.forall_intro yz_cell_lemma; matrix_mul_ijth_eq_sum_of_seq_for_init add mul mx myz i l x_yz_products_init; let x_yz_distr j : Lemma (ijth mx i j * yz_cell_as_sum j = sum_k (full_init_rh j)) = foldm_snoc_distributivity_left_eq mul add (ijth mx i j) (SB.init p (yz_products_init j)) (SB.init p (full_init_rh j)) in Classical.forall_intro x_yz_distr; SP.foldm_snoc_of_equal_inits add x_yz_products_init (fun j -> sum_k (full_init_rh j)); eq.symmetry (ijth rhs i l) (sum_jk full_init_rh); eq.transitivity (ijth lhs i l) (sum_jk full_init_rh) (ijth rhs i l); () in matrix_equiv_from_proof eq lhs rhs aux #pop-options let matrix_mul_unit_row_lemma #c #eq m (add mul: CE.cm c eq) (i: under m) : Lemma ((row (matrix_mul_unit add mul m) i == (SB.create i add.unit) `SB.append` ((SB.create 1 mul.unit) `SB.append` (SB.create (m-i-1) add.unit))) /\ (row (matrix_mul_unit add mul m) i == ((SB.create i add.unit) `SB.append` (SB.create 1 mul.unit)) `SB.append` (SB.create (m-i-1) add.unit))) = SB.lemma_eq_elim ((SB.create i add.unit `SB.append` SB.create 1 mul.unit) `SB.append` (SB.create (m-i-1) add.unit)) (row (matrix_mul_unit add mul m) i); SB.lemma_eq_elim ((SB.create i add.unit) `SB.append` (SB.create 1 mul.unit `SB.append` SB.create (m-i-1) add.unit)) (row (matrix_mul_unit add mul m) i) let matrix_mul_unit_col_lemma #c #eq m (add mul: CE.cm c eq) (i: under m) : Lemma ((col (matrix_mul_unit add mul m) i == (SB.create i add.unit) `SB.append` ((SB.create 1 mul.unit) `SB.append` (SB.create (m-i-1) add.unit))) /\ (col (matrix_mul_unit add mul m) i == ((SB.create i add.unit) `SB.append` (SB.create 1 mul.unit)) `SB.append` (SB.create (m-i-1) add.unit))) = SB.lemma_eq_elim ((SB.create i add.unit `SB.append` SB.create 1 mul.unit) `SB.append` (SB.create (m-i-1) add.unit)) (col (matrix_mul_unit add mul m) i); SB.lemma_eq_elim ((SB.create i add.unit) `SB.append` (SB.create 1 mul.unit `SB.append` SB.create (m-i-1) add.unit)) (col (matrix_mul_unit add mul m) i) let seq_of_products_zeroes_lemma #c #eq #m (mul: CE.cm c eq) (z: c{is_absorber z mul}) (s: SB.seq c{SB.length s == m}) : Lemma (ensures (eq_of_seq eq (seq_of_products mul (SB.create m z) s) (SB.create m z))) = eq_of_seq_from_element_equality eq (seq_of_products mul (SB.create m z) s) (SB.create m z) let rec foldm_snoc_zero_lemma #c #eq (add: CE.cm c eq) (zeroes: SB.seq c) : Lemma (requires (forall (i: under (SB.length zeroes)). SB.index zeroes i `eq.eq` add.unit)) (ensures eq.eq (SP.foldm_snoc add zeroes) add.unit) (decreases SB.length zeroes) = if (SB.length zeroes < 1) then begin assert_norm (SP.foldm_snoc add zeroes == add.unit); eq.reflexivity add.unit end else let liat, last = SProp.un_snoc zeroes in foldm_snoc_zero_lemma add liat; add.congruence last (SP.foldm_snoc add liat) add.unit add.unit; add.identity add.unit; SP.foldm_snoc_decomposition add zeroes; eq.transitivity (SP.foldm_snoc add zeroes) (add.mult add.unit add.unit) add.unit let matrix_mul_unit_ijth #c #eq (add mul: CE.cm c eq) m (i j: under m) : Lemma (ijth (matrix_mul_unit add mul m) i j == (if i=j then mul.unit else add.unit))=() let last_equals_index #c (s: SB.seq c{SB.length s > 0}) : Lemma ((snd (SProp.un_snoc s)) == SB.index s (SB.length s - 1)) = () let matrix_right_mul_identity_aux_0 #c #eq #m (add: CE.cm c eq) (mul: CE.cm c eq{is_absorber add.unit mul}) (mx: matrix c m m) (i j: under m) (k:nat{k=0}) : Lemma (ensures SP.foldm_snoc add (SB.init k (fun (k: under m) -> ijth mx i k `mul.mult` ijth (matrix_mul_unit add mul m) k j)) `eq.eq` add.unit) = eq.reflexivity add.unit let rec matrix_right_mul_identity_aux_1 #c #eq #m (add: CE.cm c eq) (mul: CE.cm c eq{is_absorber add.unit mul}) (mx: matrix c m m) (i j: under m) (k:nat{k<=j}) : Lemma (ensures SP.foldm_snoc add (SB.init k (fun (k: under m) -> ijth mx i k `mul.mult` ijth (matrix_mul_unit add mul m) k j)) `eq.eq` add.unit) (decreases k) = if k = 0 then matrix_right_mul_identity_aux_0 add mul mx i j k else let unit = matrix_mul_unit add mul m in let mxu = matrix_mul add mul mx unit in let ( * ) = mul.mult in let ( $=$ ) = eq.eq in let gen = fun (k: under m) -> ijth mx i k * ijth unit k j in let full = SB.init k gen in let liat,last = SProp.un_snoc full in matrix_right_mul_identity_aux_1 add mul mx i j (k-1); liat_equals_init k gen; eq.reflexivity (SP.foldm_snoc add liat); mul.congruence last (SP.foldm_snoc add liat) add.unit (SP.foldm_snoc add liat); eq.transitivity (last * SP.foldm_snoc add liat) (add.unit * SP.foldm_snoc add liat) (add.unit); eq.reflexivity (SP.foldm_snoc add (SB.init (k-1) gen)); matrix_mul_unit_ijth add mul m (k-1) j; // This one reduces the rlimits needs to default add.congruence last (SP.foldm_snoc add liat) add.unit add.unit; add.identity add.unit; SP.foldm_snoc_decomposition add full; eq.transitivity (SP.foldm_snoc add full) (add.mult add.unit add.unit) add.unit let matrix_right_mul_identity_aux_2 #c #eq #m (add: CE.cm c eq) (mul: CE.cm c eq{is_absorber add.unit mul}) (mx: matrix c m m) (i j: under m) (k:nat{k=j+1}) : Lemma (ensures SP.foldm_snoc add (SB.init k (fun (k: under m) -> ijth mx i k `mul.mult` ijth (matrix_mul_unit add mul m) k j)) `eq.eq` ijth mx i j) = let unit = matrix_mul_unit add mul m in let mxu = matrix_mul add mul mx unit in let ( * ) = mul.mult in let ( $=$ ) = eq.eq in let gen = fun (k: under m) -> ijth mx i k * ijth unit k j in let full = SB.init k gen in let liat,last = SProp.un_snoc full in matrix_right_mul_identity_aux_1 add mul mx i j j; liat_equals_init k gen; mul.identity (ijth mx i j); eq.reflexivity last; add.congruence last (SP.foldm_snoc add liat) last add.unit; matrix_mul_unit_ijth add mul m (k-1) j; // This one reduces the rlimits needs to default add.identity last; add.commutativity last add.unit; mul.commutativity (ijth mx i j) mul.unit; eq.transitivity (add.mult last add.unit) (add.mult add.unit last) last; SP.foldm_snoc_decomposition add full; eq.transitivity (SP.foldm_snoc add full) (add.mult last add.unit) last; eq.transitivity last (mul.unit * ijth mx i j) (ijth mx i j); eq.transitivity (SP.foldm_snoc add full) last (ijth mx i j) let rec matrix_right_mul_identity_aux_3 #c #eq #m (add: CE.cm c eq) (mul: CE.cm c eq{is_absorber add.unit mul}) (mx: matrix c m m) (i j: under m) (k:under (m+1){k>j+1}) : Lemma (ensures SP.foldm_snoc add (SB.init k (fun (k: under m) -> ijth mx i k `mul.mult` ijth (matrix_mul_unit add mul m) k j)) `eq.eq` ijth mx i j) (decreases k) = if (k-1) > j+1 then matrix_right_mul_identity_aux_3 add mul mx i j (k-1) else matrix_right_mul_identity_aux_2 add mul mx i j (k-1); let unit = matrix_mul_unit add mul m in let mxu = matrix_mul add mul mx unit in let ( * ) = mul.mult in let ( $=$ ) = eq.eq in let gen = fun (k: under m) -> ijth mx i k * ijth unit k j in let subgen (i: under (k)) = gen i in let full = SB.init k gen in SP.foldm_snoc_decomposition add full; liat_equals_init k gen; let liat,last = SProp.un_snoc full in SB.lemma_eq_elim liat (SB.init (k-1) gen); add.identity add.unit; mul.commutativity (ijth mx i (k-1)) add.unit; eq.reflexivity (SP.foldm_snoc add (SB.init (k-1) gen)); matrix_mul_unit_ijth add mul m (k-1) j; // This one reduces the rlimits needs to default add.congruence last (SP.foldm_snoc add (SB.init (k-1) gen)) add.unit (SP.foldm_snoc add (SB.init (k-1) gen)); add.identity (SP.foldm_snoc add (SB.init (k-1) gen)); eq.transitivity (SP.foldm_snoc add full) (add.mult add.unit (SP.foldm_snoc add (SB.init (k-1) gen))) (SP.foldm_snoc add (SB.init (k-1) gen)); eq.transitivity (SP.foldm_snoc add full) (SP.foldm_snoc add (SB.init (k-1) gen)) (ijth mx i j) let matrix_right_identity_aux #c #eq #m (add: CE.cm c eq) (mul: CE.cm c eq{is_absorber add.unit mul}) (mx: matrix c m m) (i j: under m) (k:under (m+1)) : Lemma (ensures SP.foldm_snoc add (SB.init k (fun (k: under m) -> ijth mx i k `mul.mult` ijth (matrix_mul_unit add mul m) k j)) `eq.eq` (if k>j then ijth mx i j else add.unit)) (decreases k) = if k=0 then matrix_right_mul_identity_aux_0 add mul mx i j k else if k <= j then matrix_right_mul_identity_aux_1 add mul mx i j k else if k = j+1 then matrix_right_mul_identity_aux_2 add mul mx i j k else matrix_right_mul_identity_aux_3 add mul mx i j k let matrix_left_mul_identity_aux_0 #c #eq #m (add: CE.cm c eq) (mul: CE.cm c eq{is_absorber add.unit mul}) (mx: matrix c m m) (i j: under m) (k:nat{k=0}) : Lemma (ensures SP.foldm_snoc add (SB.init k (fun (k: under m) -> ijth (matrix_mul_unit add mul m) i k `mul.mult` ijth mx k j)) `eq.eq` add.unit) = eq.reflexivity add.unit let rec matrix_left_mul_identity_aux_1 #c #eq #m (add: CE.cm c eq) (mul: CE.cm c eq{is_absorber add.unit mul}) (mx: matrix c m m) (i j: under m) (k:nat{k<=i /\ k>0}) : Lemma (ensures SP.foldm_snoc add (SB.init k (fun (k: under m) -> ijth (matrix_mul_unit add mul m) i k `mul.mult` ijth mx k j)) `eq.eq` add.unit) = let unit = matrix_mul_unit add mul m in let mxu = matrix_mul add mul mx unit in let ( * ) = mul.mult in let ( $=$ ) = eq.eq in let gen (k: under m) = ijth unit i k * ijth mx k j in let full = SB.init k gen in let liat,last = SProp.un_snoc full in if k=1 then matrix_left_mul_identity_aux_0 add mul mx i j (k-1) else matrix_left_mul_identity_aux_1 add mul mx i j (k-1); liat_equals_init k gen; eq.reflexivity (SP.foldm_snoc add liat); SP.foldm_snoc_decomposition add full; mul.congruence last (SP.foldm_snoc add liat) add.unit (SP.foldm_snoc add liat); eq.transitivity (last * SP.foldm_snoc add liat) (add.unit * SP.foldm_snoc add liat) (add.unit); add.congruence last (SP.foldm_snoc add liat) add.unit add.unit; add.identity add.unit; eq.transitivity (SP.foldm_snoc add full) (add.mult add.unit add.unit) add.unit
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Seq.Properties.fsti.checked", "FStar.Seq.Permutation.fsti.checked", "FStar.Seq.Equiv.fsti.checked", "FStar.Seq.Base.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.IntegerIntervals.fst.checked", "FStar.Classical.fsti.checked", "FStar.Algebra.CommMonoid.Fold.fsti.checked", "FStar.Algebra.CommMonoid.Equiv.fst.checked" ], "interface_file": true, "source_file": "FStar.Matrix.fst" }
[ { "abbrev": false, "full_module": "FStar.Seq.Equiv", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "ML" }, { "abbrev": true, "full_module": "FStar.Seq.Properties", "short_module": "SProp" }, { "abbrev": true, "full_module": "FStar.Seq.Base", "short_module": "SB" }, { "abbrev": true, "full_module": "FStar.Seq.Permutation", "short_module": "SP" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Fold", "short_module": "CF" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.IntegerIntervals", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "ML" }, { "abbrev": true, "full_module": "FStar.Seq.Base", "short_module": "SB" }, { "abbrev": true, "full_module": "FStar.Seq.Permutation", "short_module": "SP" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Fold", "short_module": "CF" }, { "abbrev": true, "full_module": "FStar.Algebra.CommMonoid.Equiv", "short_module": "CE" }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_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
add: FStar.Algebra.CommMonoid.Equiv.cm c eq -> mul: FStar.Algebra.CommMonoid.Equiv.cm c eq {FStar.Matrix.is_absorber (CM?.unit add) mul} -> mx: FStar.Matrix.matrix c m m -> i: FStar.IntegerIntervals.under m -> j: FStar.IntegerIntervals.under m -> k: Prims.nat{k = i + 1} -> FStar.Pervasives.Lemma (ensures EQ?.eq eq (FStar.Seq.Permutation.foldm_snoc add (FStar.Seq.Base.init k (fun k -> CM?.mult mul (FStar.Matrix.ijth (FStar.Matrix.matrix_mul_unit add mul m) i k) (FStar.Matrix.ijth mx k j)))) (FStar.Matrix.ijth mx i j))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Algebra.CommMonoid.Equiv.equiv", "Prims.pos", "FStar.Algebra.CommMonoid.Equiv.cm", "FStar.Matrix.is_absorber", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit", "FStar.Matrix.matrix", "FStar.IntegerIntervals.under", "Prims.nat", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Prims.op_Addition", "FStar.Seq.Base.seq", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__transitivity", "FStar.Seq.Permutation.foldm_snoc", "FStar.Matrix.ijth", "Prims.unit", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__commutativity", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__identity", "FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__congruence", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__reflexivity", "FStar.Matrix.liat_equals_init", "FStar.Seq.Permutation.foldm_snoc_decomposition", "FStar.Matrix.matrix_mul_unit_ijth", "Prims.op_Subtraction", "FStar.Matrix.matrix_left_mul_identity_aux_0", "Prims.bool", "FStar.Matrix.matrix_left_mul_identity_aux_1", "Prims._assert", "Prims.l_and", "Prims.op_LessThanOrEqual", "Prims.op_GreaterThanOrEqual", "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.Base.init", "FStar.Algebra.CommMonoid.Equiv.__proj__EQ__item__eq", "FStar.Matrix.matrix_mul", "FStar.Matrix.matrix_mul_unit", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let matrix_left_mul_identity_aux_2 #c #eq #m (add: CE.cm c eq) (mul: CE.cm c eq {is_absorber add.unit mul}) (mx: matrix c m m) (i: under m) (j: under m) (k: nat{k = i + 1}) : Lemma (ensures (SP.foldm_snoc add (SB.init k (fun (k: under m) -> (ijth (matrix_mul_unit add mul m) i k) `mul.mult` (ijth mx k j))) ) `eq.eq` (ijth mx i j)) =
let unit = matrix_mul_unit add mul m in let mxu = matrix_mul add mul mx unit in let ( * ) = mul.mult in let ( $=$ ) = eq.eq in let gen (k: under m) = ijth unit i k * ijth mx k j in let full = SB.init k gen in let liat, last = SProp.un_snoc full in assert (k - 1 <= i /\ k - 1 >= 0); if (k - 1) = 0 then matrix_left_mul_identity_aux_0 add mul mx i j (k - 1) else matrix_left_mul_identity_aux_1 add mul mx i j (k - 1); matrix_mul_unit_ijth add mul m i (k - 1); SP.foldm_snoc_decomposition add full; liat_equals_init k gen; mul.identity (ijth mx i j); eq.reflexivity last; add.congruence last (SP.foldm_snoc add liat) last add.unit; add.identity last; add.commutativity last add.unit; mul.commutativity (ijth mx i j) mul.unit; eq.transitivity (add.mult last add.unit) (add.mult add.unit last) last; eq.transitivity (SP.foldm_snoc add full) (add.mult last add.unit) last; eq.transitivity last (mul.unit * ijth mx i j) (ijth mx i j); eq.transitivity (SP.foldm_snoc add full) last (ijth mx i j)
false
AlgWP.fst
AlgWP.__tree_handle_into_ro
val __tree_handle_into_ro (#a: _) (#l: rwops{~(List.Tot.memP Write l)}) (#wp: _) (f: repr a (Write :: l) wp) : repr a l null_ro
val __tree_handle_into_ro (#a: _) (#l: rwops{~(List.Tot.memP Write l)}) (#wp: _) (f: repr a (Write :: l) wp) : repr a l null_ro
let __tree_handle_into_ro #a (#l:rwops{~(List.Tot.memP Write l)}) #wp (f : repr a (Write::l) wp) : repr a l null_ro = let f' : tree a (Write::l) = f in let h : tree a l = handle_tree f' (fun x -> Return x) (function Write -> fun i k -> k () | op -> fun i k -> Op op i k) in assert (sublist l [Read]); let h : tree a [Read] = h in null_ro_tree_wp h; h
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 395, "start_col": 0, "start_line": 384 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x)) let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c let quot #a #wp (f : unit -> AlgWP a [Read] wp) : AlgWP a [Read] (quotient_ro wp) = AlgWP?.reflect (quot_tree (reify (f ()))) effect AlgPP (a:Type) (ll:rwops) (pre : state -> Type0) (post : state -> a -> state -> Type0) = AlgWP a ll (fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) let quotPP #a #pre #post (f : unit -> AlgPP a [Read] pre post) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1) = quot #_ #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f let null #a : st_wp a = fun s0 p -> forall r. p r let null_ro #a : st_wp a = quotient_ro null let null_ro1 #a : st_wp a = fun s0 p -> forall x. p (x, s0) let null_equiv_sanity a = assert (null_ro #a `equiv` null_ro1 #a) let bind_null_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires (null_ro `stronger` w) /\ (forall x. null_ro `stronger` f x)) (ensures null_ro `stronger` (bind_wp w f)) = () (* Similar to ro_tree_wp *) let rec null_ro_tree_wp #a (t : tree a [Read]) : Lemma (null_ro `stronger` (interp_as_wp t)) by (T.compute ()) // need this to trigger some unfoldings = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (null_ro `stronger` interp_as_wp (k x)) = null_ro_tree_wp (k x) in Classical.forall_intro aux; bind_null_ro read_wp (fun x -> interp_as_wp (k x)) (* Could this work automatically too? *) // ro_tree_wp (reify (f ())); // f ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: AlgWP.repr a (Alg.Write :: l) wp -> AlgWP.repr a l AlgWP.null_ro
Prims.Tot
[ "total" ]
[]
[ "AlgWP.rwops", "Prims.l_not", "FStar.List.Tot.Base.memP", "Alg.op", "Alg.Write", "AlgWP.st_wp", "AlgWP.repr", "Prims.Cons", "Prims.unit", "AlgWP.null_ro_tree_wp", "Alg.tree", "Alg.Read", "Prims.Nil", "Prims._assert", "Alg.sublist", "Alg.handle_tree", "Alg.Return", "Alg.op_inp", "Alg.op_out", "Alg.Op", "Alg.handler_tree_op", "AlgWP.null_ro" ]
[]
false
false
false
false
false
let __tree_handle_into_ro #a (#l: rwops{~(List.Tot.memP Write l)}) #wp (f: repr a (Write :: l) wp) : repr a l null_ro =
let f':tree a (Write :: l) = f in let h:tree a l = handle_tree f' (fun x -> Return x) (function | Write -> fun i k -> k () | op -> fun i k -> Op op i k) in assert (sublist l [Read]); let h:tree a [Read] = h in null_ro_tree_wp h; h
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_from_to_mont_id_gen
val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a)
val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a)
let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 49, "end_line": 51, "start_col": 0, "start_line": 50 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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
n: Prims.pos -> mont_R: Prims.pos -> mont_R_inv: Prims.pos -> a: Prims.nat{a < n} -> FStar.Pervasives.Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "Hacl.Spec.P256.Montgomery.lemma_to_from_mont_id_gen", "Prims.unit" ]
[]
true
false
true
false
false
let lemma_from_to_mont_id_gen n mont_R mont_R_inv a =
lemma_to_from_mont_id_gen n mont_R_inv mont_R a
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_mod_mul_assoc
val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n)
val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n)
let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; }
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 34, "start_col": 0, "start_line": 25 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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
n: Prims.pos -> a: Prims.nat -> b: Prims.nat -> c: Prims.nat -> FStar.Pervasives.Lemma (ensures (a * b % n) * c % n == a * (b * c % n) % n)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "Prims.nat", "FStar.Calc.calc_finish", "Prims.int", "Prims.eq2", "Prims.op_Modulus", "FStar.Mul.op_Star", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Math.Lemmas.lemma_mod_mul_distr_l", "Prims.squash", "FStar.Math.Lemmas.paren_mul_right", "FStar.Math.Lemmas.lemma_mod_mul_distr_r" ]
[]
false
false
true
false
false
let lemma_mod_mul_assoc m a b c =
calc ( == ) { (a * b % m) * c % m; ( == ) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; ( == ) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; ( == ) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; }
false
MerkleTree.New.High.fst
MerkleTree.New.High.sha256_compress
val sha256_compress: src1:hash #32 -> src2:hash #32 -> GTot (hash #32)
val sha256_compress: src1:hash #32 -> src2:hash #32 -> GTot (hash #32)
let sha256_compress = MTS.sha256_compress
{ "file_name": "src/MerkleTree.New.High.fst", "git_rev": "7d7bdc20f2033171e279c176b26e84f9069d23c6", "git_url": "https://github.com/hacl-star/merkle-tree.git", "project_name": "merkle-tree" }
{ "end_col": 41, "end_line": 27, "start_col": 0, "start_line": 27 }
module MerkleTree.New.High open FStar.Ghost open FStar.Seq module S = FStar.Seq module U32 = FStar.UInt32 module U8 = FStar.UInt8 module MTS = MerkleTree.Spec #set-options "--z3rlimit 10 --max_fuel 0 --max_ifuel 0" type uint32_t = U32.t type uint8_t = U8.t type hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes{Seq.length b = hsz} type hashes (#hsz:pos) = S.seq (hash #hsz) type hashess (#hsz:pos) = S.seq (hashes #hsz) noextract let hash_init (#hsz:pos): hash #hsz = Seq.create hsz (Lib.IntTypes.u8 0)
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "MerkleTree.Spec.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "MerkleTree.New.High.fst" }
[ { "abbrev": true, "full_module": "MerkleTree.Spec", "short_module": "MTS" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "MerkleTree.New", "short_module": null }, { "abbrev": false, "full_module": "MerkleTree.New", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
src1: MerkleTree.New.High.hash -> src2: MerkleTree.New.High.hash -> Prims.GTot MerkleTree.New.High.hash
Prims.GTot
[ "sometrivial" ]
[]
[ "MerkleTree.Spec.sha256_compress" ]
[]
false
false
false
false
false
let sha256_compress =
MTS.sha256_compress
false
AlgWP.fst
AlgWP.quotPP
val quotPP (#a #pre #post: _) (f: (unit -> AlgPP a [Read] pre post)) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1)
val quotPP (#a #pre #post: _) (f: (unit -> AlgPP a [Read] pre post)) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1)
let quotPP #a #pre #post (f : unit -> AlgPP a [Read] pre post) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1) = quot #_ #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 80, "end_line": 355, "start_col": 0, "start_line": 353 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x)) let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c let quot #a #wp (f : unit -> AlgWP a [Read] wp) : AlgWP a [Read] (quotient_ro wp) = AlgWP?.reflect (quot_tree (reify (f ()))) effect AlgPP (a:Type) (ll:rwops) (pre : state -> Type0) (post : state -> a -> state -> Type0) = AlgWP a ll (fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: Prims.unit -> AlgWP.AlgPP a) -> AlgWP.AlgPP a
AlgWP.AlgPP
[]
[]
[ "Alg.state", "Prims.unit", "Prims.Cons", "Alg.op", "Alg.Read", "Prims.Nil", "AlgWP.quot", "FStar.Pervasives.Native.tuple2", "Prims.logical", "Prims.l_and", "Prims.l_Forall", "Prims.l_imp", "FStar.Pervasives.Native.Mktuple2", "Prims.eq2" ]
[]
false
true
false
false
false
let quotPP #a #pre #post (f: (unit -> AlgPP a [Read] pre post)) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1) =
quot #_ #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_to_from_mont_id_gen
val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a)
val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a)
let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 30, "end_line": 43, "start_col": 0, "start_line": 41 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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
n: Prims.pos -> mont_R: Prims.pos -> mont_R_inv: Prims.pos -> a: Prims.nat{a < n} -> FStar.Pervasives.Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.Math.Lemmas.modulo_lemma", "Prims.unit", "Hacl.Spec.P256.Montgomery.lemma_mod_mul_assoc" ]
[]
true
false
true
false
false
let lemma_to_from_mont_id_gen n mont_R mont_R_inv a =
lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n
false
AlgWP.fst
AlgWP.null_ro_tree_wp
val null_ro_tree_wp (#a: _) (t: tree a [Read]) : Lemma (null_ro `stronger` (interp_as_wp t))
val null_ro_tree_wp (#a: _) (t: tree a [Read]) : Lemma (null_ro `stronger` (interp_as_wp t))
let rec null_ro_tree_wp #a (t : tree a [Read]) : Lemma (null_ro `stronger` (interp_as_wp t)) by (T.compute ()) // need this to trigger some unfoldings = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (null_ro `stronger` interp_as_wp (k x)) = null_ro_tree_wp (k x) in Classical.forall_intro aux; bind_null_ro read_wp (fun x -> interp_as_wp (k x))
{ "file_name": "examples/layeredeffects/AlgWP.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 56, "end_line": 378, "start_col": 0, "start_line": 368 }
module AlgWP (* AlgWP: tracking operation labels and WPs. At the end, we show how we can recover semantic facts from the labels alone, e.g. that interpreting a tree will not change the state, effectively allowing to strengthen a WP from intensional information about the operations in the tree. *) open FStar.List.Tot open FStar.Calc module FE = FStar.FunctionalExtensionality module F = FStar.FunctionalExtensionality module W = FStar.WellFounded module T = FStar.Tactics.V2 module ID5 = ID5 open Alg let rwops = labs:ops{sublist labs [Read; Write]} let noops : rwops = [] type rwtree a (l : ops{l `sublist` [Read;Write]}) = Alg.tree a l (* Somehow did not need this in Alg! *) let rec sublist_at_const (l1 l2 l3 : ops) : Lemma (requires (sublist l1 l3 /\ sublist l2 l3)) (ensures (sublist (l1@l2) l3)) [SMTPat (sublist (l1@l2) l3)] = match l1 with | [] -> () | h::t -> sublist_at_const t l2 l3 let (@@) : rwops -> rwops -> rwops = fun l1 l2 -> l1@l2 let subops : rwops -> rwops -> Type0 = sublist let sublist_at (l1 l2 : ops) : Lemma (sublist l1 (l1@l2) /\ sublist l2 (l1@l2)) [SMTPat (l1@l2)] = Alg.sublist_at l1 l2 let rwtree_help a labs (t : rwtree a labs) : Lemma (forall l. l `List.Tot.memP` labs ==> l == Read \/ l == Write) [SMTPat (has_type t (rwtree a labs))] = () let tbind : #a:_ -> #b:_ -> #labs1:_ -> #labs2:_ -> rwtree a labs1 -> (a -> rwtree b labs2) -> rwtree b (labs1@@labs2) = fun c f -> Alg.bind _ _ c f let st_wp0 (a:Type) : Type = state -> (a & state -> Type0) -> Type0 let st_monotonic #a (w : st_wp0 a) : Type0 = //forall s0 p1 p2. (forall r. p1 r ==> p2 r) ==> w s0 p1 ==> w s0 p2 // ^ this version seems to be less SMT-friendly forall s0 p1 p2. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> w s0 p1 ==> w s0 p2 let st_wp (a:Type) = wp:st_wp0 a{st_monotonic wp} unfold let return_wp #a x : st_wp a = fun s0 p -> p (x, s0) unfold let bind_wp #a #b (w : st_wp a) (wf : a -> st_wp b) : st_wp b = fun s0 p -> w s0 (fun (y, s1) -> wf y s1 p) unfold let read_wp : st_wp state = fun s0 p -> p (s0, s0) unfold let write_wp : state -> st_wp unit = fun s _ p -> p ((), s) (* Also doable with handlers *) let rec interp_as_wp #a (t : Alg.tree a [Read;Write]) : st_wp a = match t with | Return x -> return_wp x | Op Read _ k -> bind_wp read_wp (fun s -> interp_as_wp (k s)) | Op Write s k -> bind_wp (write_wp s) (fun (o:unit) -> interp_as_wp (k o)) (* With handlers. Can only be done into []? See the use of `run`. *) let interp_as_wp2 #a #l (t : rwtree a l) : Alg (st_wp a) [] = let t0 : Alg.tree a [Read; Write] = t in handle_with #a #(st_wp a) #[Read; Write] #[] (fun () -> Alg?.reflect t0) (fun x -> return_wp x) (function Read -> (fun i k -> bind_wp read_wp (fun s -> run (fun () -> k s))) | Write -> (fun i k -> bind_wp (write_wp i) (fun _ -> run k))) (* Bug: defining this as a FStar.Preorder.preorder causes stupid failures ahead *) unfold val stronger : (#a:Type) -> st_wp a -> st_wp a -> Type0 let stronger w1 w2 = forall p s. w1 p s ==> w2 p s let equiv #a (w1 w2 : st_wp a) = w1 `stronger` w2 /\ w2 `stronger` w1 let (<<=) = stronger val interp_ret (#a:Type) (#l:rwops) (x:a) : Lemma (return_wp x `stronger` interp_as_wp (Return x)) let interp_ret x = () val interp_ret' (#a:Type) (x:a) : Lemma (return_wp x == interp_as_wp (Return x)) let interp_ret' x = assert_norm (return_wp x == interp_as_wp (Return x)) let wp_is_monotonic #a (wp : st_wp a) : Type0 = forall p1 p2 s0. (forall x s1. p1 (x, s1) ==> p2 (x, s1)) ==> wp s0 p1 ==> wp s0 p2 let bind_preserves_mon #a #b (wp : st_wp a) (f : a -> st_wp b) : Lemma (requires (wp_is_monotonic wp /\ (forall x. wp_is_monotonic (f x)))) (ensures (wp_is_monotonic (bind_wp wp f))) = () let rec interp_monotonic #a #l (c:rwtree a l) : Lemma (wp_is_monotonic (interp_as_wp c)) = match c with | Return x -> () | Op Read _ k -> let aux (x:state) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon read_wp (fun x -> interp_as_wp (k x)) | Op Write s k -> let aux (x:unit) : Lemma (wp_is_monotonic (interp_as_wp (k x))) = interp_monotonic #_ #l (k x) in Classical.forall_intro aux; bind_preserves_mon (write_wp s) (fun x -> interp_as_wp (k x)) let elim_str #a (w1 w2 : st_wp a) (p : (a & state -> Type0)) (s0:state) : Lemma (requires (w1 <<= w2)) (ensures w1 s0 p ==> w2 s0 p) = () #set-options "--print_implicits" (* Takes a while, known to fail sporadically *) #push-options "--retry 10" let rec interp_morph #a #b #l1 #l2 (c : rwtree a l1) (f : a -> rwtree b l2) (p:_) (s0:_) : Lemma (interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 c f) s0 p) = match c with | Return x -> interp_ret #_ #l2 x | Op Read _ k -> let aux (o:state) : Lemma (interp_as_wp (k o) s0 (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s0 p) = interp_morph #_ #_ #l1 #l2 (k o) f p s0 in Classical.forall_intro aux | Op Write s k -> let aux (o:unit) : Lemma (interp_as_wp (k o) s (fun (y, s1) -> interp_as_wp (f y) s1 p) == interp_as_wp (tbind #_ #_ #l1 #l2 (k o) f) s p) = interp_morph #_ #_ #l1 #l2 (k o) f p s in Classical.forall_intro aux | _ -> () // this branch is unreachable #pop-options val interp_bind (#a #b:Type) (#l1 #l2 : rwops) (c : rwtree a l1) (f : a -> rwtree b l2) (w1 : st_wp a) (w2 : a -> st_wp b) : Lemma (requires w1 <<= interp_as_wp c /\ (forall x. w2 x <<= interp_as_wp (f x))) (ensures bind_wp w1 w2 `stronger` interp_as_wp (tbind c f)) let interp_bind #a #b c f w1 w2 = let aux (p: (b & state -> Type0)) (s0:state) : Lemma (bind_wp w1 w2 s0 p ==> interp_as_wp (tbind c f) s0 p) = calc (==>) { bind_wp w1 w2 s0 p; ==> {} w1 s0 (fun (y, s1) -> w2 y s1 p); ==> { (* hyp *)} interp_as_wp c s0 (fun (y, s1) -> w2 y s1 p); ==> { interp_monotonic c } interp_as_wp c s0 (fun (y, s1) -> interp_as_wp (f y) s1 p); ==> { interp_morph c f p s0 } interp_as_wp (tbind c f) s0 p; } in Classical.forall_intro_2 aux let repr (a : Type) (l : rwops) (w: st_wp a) = c:(rwtree a l){w `stronger` interp_as_wp c} let return (a:Type) (x:a) : repr a noops (return_wp x) = interp_ret #_ #[] x; Return x let bind (a : Type) (b : Type) (#l1 : rwops) (#wp_v : st_wp a) (#l2 : rwops) (#wp_f: a -> st_wp b) (v : repr a l1 wp_v) (f : (x:a -> repr b l2 (wp_f x))) : repr b (l1@@l2) (bind_wp wp_v wp_f) = interp_bind v f wp_v wp_f; tbind v f let subcomp (a:Type) (#l1 : rwops) (#w1 : st_wp a) (#l2 : rwops) (#w2: st_wp a) (f : repr a l1 w1) : Pure (repr a l2 w2) (requires w2 `stronger` w1 /\ l1 `subops` l2) (ensures fun _ -> True) = f let if_then_else (a : Type) (l1 : rwops) (w1 : st_wp a) (l2 : rwops) (w2 : st_wp a) (f : repr a l1 w1) (g : repr a l2 w2) (b : bool) : Type = repr a (l1@@l2) (if b then w1 else w2) total reifiable reflectable effect { AlgWP (a:Type) (_:rwops) (_:st_wp a) with {repr; return; bind; subcomp; if_then_else} } let get () : AlgWP state [Read] read_wp = AlgWP?.reflect (Op Read () Return) let put (s:state) : AlgWP unit [Write] (write_wp s) = AlgWP?.reflect (Op Write s Return) unfold let lift_pure_wp (#a:Type) (wp : pure_wp a) : st_wp a = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun s0 p -> wp (fun x -> p (x, s0)) let lift_pure_algwp (a:Type) wp (f:unit -> PURE a wp) : Pure (repr a noops (lift_pure_wp wp)) // can't call f() here, so lift its wp instead (requires (wp (fun _ -> True))) (ensures (fun _ -> True)) = let v : a = FStar.Monotonic.Pure.elim_pure f (fun _ -> True) in FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; // need this lemma assert (forall p. wp p ==> p v); // this is key fact needed for the proof assert_norm (stronger (lift_pure_wp wp) (return_wp v)); Return v sub_effect PURE ~> AlgWP = lift_pure_algwp let addx (x:int) : AlgWP unit [Read; Write] (fun s0 p -> p ((), (s0+x))) = let y = get () in put (x+y) (* GM: this used to require a call to T.norm [delta] when I had curry/uncurry going on. I now realize they were not marked unfold, but that is pretty tricky... we should try to find some general solution for these things. *) let add_via_state (x y : int) : AlgWP int [Read;Write] (fun s0 p -> p ((x+y), s0)) = let o = get () in put x; addx y; let r = get () in put o; r open FStar.Monotonic.Pure let rec interp_sem #a (t : rwtree a [Read; Write]) (s0:state) : ID5.ID (a & state) (as_pure_wp (interp_as_wp t s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_sem (k s0) s0 | Op Write i k -> interp_sem (k ()) i let quotient_ro #a (w : st_wp a) : st_wp a = fun s0 p -> w s0 (fun (y, s1) -> s0 == s1 ==> p (y, s1)) let is_mono #a (w : st_wp a) : Type0 = forall s0 p1 p2. (forall x. p1 x ==> p2 x) ==> w s0 p1 ==> w s0 p2 let is_ro #a (w : st_wp a) : Type0 = quotient_ro w `stronger` w let sanity_1 = assert (forall s0 p. quotient_ro read_wp s0 p <==> read_wp s0 p) let sanity_2 = assert (forall s0 p s1. p ((), s0) ==> quotient_ro (write_wp s1) s0 p) #push-options "--z3rlimit 20" let rec interp_ro #a (t : rwtree a [Read]) (s0:state) : ID5.ID (a & state) (as_pure_wp (quotient_ro (interp_as_wp t) s0)) = match t with | Return x -> (x, s0) | Op Read i k -> interp_ro (k s0) s0 #pop-options let st_soundness_aux #a #wp (t : repr a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = interp_sem t let st_soundness #a #wp (t : unit -> AlgWP a [Read; Write] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (wp s0))) = st_soundness_aux (reify (t ())) (* This guarantees the final state is unchanged, but see below for an alternative statement. *) let ro_soundness_aux #a #wp ($t : repr a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = interp_ro t let ro_soundness #a #wp ($t : unit -> AlgWP a [Read] wp) : Tot (s0:state -> ID5.ID (a & state) (as_pure_wp (quotient_ro wp s0))) = ro_soundness_aux (reify (t ())) (****** Internalizing the relation between the labels and the WP. *) let ro_soundness_pre_post #a #wp (t : unit -> AlgWP a [Read] wp) (s0:state) : ID5.Id (a & state) (requires (wp s0 (fun _ -> True))) (ensures (fun (r, s1) -> s0 == s1)) = ro_soundness t s0 let bind_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires is_ro w /\ (forall x. is_ro (f x))) (ensures is_ro (bind_wp w f)) = () let quot_mono #a #b (w1 w2 : st_wp a) : Lemma (requires w1 `stronger` w2) (ensures quotient_ro w1 `stronger` quotient_ro w2) = () let rec ro_tree_wp #a (t : tree a [Read]) : Lemma (is_ro (interp_as_wp t)) = match t with | Return x -> () | Op Read i k -> let aux (x:state) : Lemma (is_ro (interp_as_wp (k x))) = ro_tree_wp (k x) in Classical.forall_intro aux; bind_ro read_wp (fun x -> interp_as_wp (k x)) let quot_tree #a #wp (c : repr a [Read] wp) : repr a [Read] (quotient_ro wp) = ro_tree_wp c; c let quot #a #wp (f : unit -> AlgWP a [Read] wp) : AlgWP a [Read] (quotient_ro wp) = AlgWP?.reflect (quot_tree (reify (f ()))) effect AlgPP (a:Type) (ll:rwops) (pre : state -> Type0) (post : state -> a -> state -> Type0) = AlgWP a ll (fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) let quotPP #a #pre #post (f : unit -> AlgPP a [Read] pre post) : AlgPP a [Read] pre (fun h0 x h1 -> post h0 x h1 /\ h0 == h1) = quot #_ #(fun h0 p -> pre h0 /\ (forall y h1. post h0 y h1 ==> p (y, h1))) f let null #a : st_wp a = fun s0 p -> forall r. p r let null_ro #a : st_wp a = quotient_ro null let null_ro1 #a : st_wp a = fun s0 p -> forall x. p (x, s0) let null_equiv_sanity a = assert (null_ro #a `equiv` null_ro1 #a) let bind_null_ro #a #b (w : st_wp a) (f : a -> st_wp b) : Lemma (requires (null_ro `stronger` w) /\ (forall x. null_ro `stronger` f x)) (ensures null_ro `stronger` (bind_wp w f)) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "ID5.fst.checked", "FStar.WellFounded.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked", "FStar.Calc.fsti.checked", "Alg.fst.checked" ], "interface_file": false, "source_file": "AlgWP.fst" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "Alg", "short_module": null }, { "abbrev": true, "full_module": "ID5", "short_module": "ID5" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": true, "full_module": "FStar.WellFounded", "short_module": "W" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "F" }, { "abbrev": true, "full_module": "FStar.FunctionalExtensionality", "short_module": "FE" }, { "abbrev": false, "full_module": "FStar.Calc", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "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: Alg.tree a [Alg.Read] -> FStar.Pervasives.Lemma (ensures AlgWP.stronger AlgWP.null_ro (AlgWP.interp_as_wp t))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Alg.tree", "Prims.Cons", "Alg.op", "Alg.Read", "Prims.Nil", "Alg.op_inp", "Alg.op_out", "Alg.tree0", "AlgWP.bind_null_ro", "Alg.state", "AlgWP.read_wp", "AlgWP.interp_as_wp", "AlgWP.st_wp", "Prims.unit", "FStar.Classical.forall_intro", "AlgWP.stronger", "AlgWP.null_ro", "Prims.l_True", "Prims.squash", "Prims.l_Forall", "FStar.Pervasives.Native.tuple2", "Prims.l_imp", "FStar.Pervasives.pattern", "AlgWP.null_ro_tree_wp", "FStar.Tactics.V2.Derived.compute" ]
[ "recursion" ]
false
false
true
false
false
let rec null_ro_tree_wp #a (t: tree a [Read]) : Lemma (null_ro `stronger` (interp_as_wp t)) by (T.compute ()) =
match t with | Return x -> () | Op Read i k -> let aux (x: state) : Lemma (null_ro `stronger` (interp_as_wp (k x))) = null_ro_tree_wp (k x) in Classical.forall_intro aux; bind_null_ro read_wp (fun x -> interp_as_wp (k x))
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.mont_add_lemma_gen
val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n)
val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n)
let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; }
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 93, "start_col": 0, "start_line": 84 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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
n: Prims.pos -> mont_R_inv: Prims.nat -> a: Prims.nat -> b: Prims.nat -> FStar.Pervasives.Lemma (ensures (a * mont_R_inv % n + b * mont_R_inv % n) % n == ((a + b) % n) * mont_R_inv % n)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "Prims.nat", "FStar.Calc.calc_finish", "Prims.int", "Prims.eq2", "Prims.op_Modulus", "Prims.op_Addition", "FStar.Mul.op_Star", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Math.Lemmas.modulo_distributivity", "Prims.squash", "FStar.Math.Lemmas.distributivity_add_left", "FStar.Math.Lemmas.lemma_mod_mul_distr_l" ]
[]
false
false
true
false
false
let mont_add_lemma_gen n mont_R_inv a b =
calc ( == ) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; ( == ) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; ( == ) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; ( == ) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } ((a + b) % n) * mont_R_inv % n; }
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.fmont_R_inv
val fmont_R_inv : pos
val fmont_R_inv : pos
let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 57, "end_line": 161, "start_col": 0, "start_line": 160 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; }
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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.pos
Prims.Tot
[ "total" ]
[]
[ "Prims.int", "Prims.op_Modulus", "Spec.P256.PointOps.prime", "Prims.pos", "FStar.Pervasives.Native.tuple2", "Hacl.Spec.Montgomery.Lemmas.eea_pow2_odd" ]
[]
false
false
false
true
false
let fmont_R_inv =
let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.fmont_sub_lemma
val fmont_sub_lemma: a:S.felem -> b:S.felem -> Lemma (S.fsub (from_mont a) (from_mont b) = from_mont ((a - b) % S.prime))
val fmont_sub_lemma: a:S.felem -> b:S.felem -> Lemma (S.fsub (from_mont a) (from_mont b) = from_mont ((a - b) % S.prime))
let fmont_sub_lemma a b = mont_sub_lemma_gen S.prime fmont_R_inv a b
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 44, "end_line": 230, "start_col": 0, "start_line": 229 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime //--------------------------------------// // bn_mont_reduction is x * fmont_R_inv // //--------------------------------------// val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0) let lemma_prime_mont () = assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0) let bn_mont_reduction_lemma x n = lemma_prime_mont (); assert (SBM.bn_mont_pre n (u64 1)); let d, _ = SBML.eea_pow2_odd 256 (BD.bn_v n) in let res = SBM.bn_mont_reduction n (u64 1) x in assert_norm (S.prime * S.prime < S.prime * pow2 256); assert (BD.bn_v x < S.prime * pow2 256); SBM.bn_mont_reduction_lemma n (u64 1) x; assert (BD.bn_v res == SBML.mont_reduction 64 4 (BD.bn_v n) 1 (BD.bn_v x)); SBML.mont_reduction_lemma 64 4 (BD.bn_v n) 1 (BD.bn_v x); assert (BD.bn_v res == BD.bn_v x * d % S.prime); calc (==) { BD.bn_v x * d % S.prime; (==) { Math.Lemmas.lemma_mod_mul_distr_r (BD.bn_v x) d S.prime } BD.bn_v x * (d % S.prime) % S.prime; (==) { } BD.bn_v x * fmont_R_inv % S.prime; } //--------------------------- let lemma_from_mont_zero a = Spec.P256.Lemmas.prime_lemma (); Lib.NatMod.lemma_mul_mod_prime_zero #S.prime a fmont_R_inv let lemma_to_from_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_to_from_mont_id_gen S.prime fmont_R fmont_R_inv a let lemma_from_to_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_from_to_mont_id_gen S.prime fmont_R fmont_R_inv a let fmont_mul_lemma a b = mont_mul_lemma_gen S.prime fmont_R_inv a b let fmont_add_lemma a b = mont_add_lemma_gen S.prime fmont_R_inv a b
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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: Spec.P256.PointOps.felem -> b: Spec.P256.PointOps.felem -> FStar.Pervasives.Lemma (ensures Spec.P256.PointOps.fsub (Hacl.Spec.P256.Montgomery.from_mont a) (Hacl.Spec.P256.Montgomery.from_mont b) = Hacl.Spec.P256.Montgomery.from_mont ((a - b) % Spec.P256.PointOps.prime))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.P256.PointOps.felem", "Hacl.Spec.P256.Montgomery.mont_sub_lemma_gen", "Spec.P256.PointOps.prime", "Hacl.Spec.P256.Montgomery.fmont_R_inv", "Prims.unit" ]
[]
true
false
true
false
false
let fmont_sub_lemma a b =
mont_sub_lemma_gen S.prime fmont_R_inv a b
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.mont_mul_lemma_gen
val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n)
val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n)
let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; }
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 78, "start_col": 0, "start_line": 58 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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
n: Prims.pos -> mont_R_inv: Prims.nat -> a: Prims.nat -> b: Prims.nat -> FStar.Pervasives.Lemma (ensures (a * mont_R_inv % n) * (b * mont_R_inv % n) % n == ((a * b) * mont_R_inv % n) * mont_R_inv % n )
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "Prims.nat", "FStar.Calc.calc_finish", "Prims.int", "Prims.eq2", "Prims.op_Modulus", "FStar.Mul.op_Star", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Math.Lemmas.lemma_mod_mul_distr_l", "Prims.squash", "FStar.Math.Lemmas.lemma_mod_mul_distr_r", "FStar.Math.Lemmas.paren_mul_right", "FStar.Math.Lemmas.swap_mul" ]
[]
false
false
true
false
false
let mont_mul_lemma_gen n mont_R_inv a b =
calc ( == ) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; ( == ) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } ((a * mont_R_inv) * (b * mont_R_inv % n)) % n; ( == ) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } ((a * mont_R_inv) * (b * mont_R_inv)) % n; ( == ) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; ( == ) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * ((mont_R_inv * b) * mont_R_inv)) % n; ( == ) { Math.Lemmas.swap_mul mont_R_inv b } (a * ((b * mont_R_inv) * mont_R_inv)) % n; ( == ) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } ((a * (b * mont_R_inv)) * mont_R_inv) % n; ( == ) { Math.Lemmas.paren_mul_right a b mont_R_inv } (((a * b) * mont_R_inv) * mont_R_inv) % n; ( == ) { Math.Lemmas.lemma_mod_mul_distr_l ((a * b) * mont_R_inv) mont_R_inv n } (((a * b) * mont_R_inv) % n) * mont_R_inv % n; }
false
MerkleTree.New.High.fst
MerkleTree.New.High.mt_not_empty
val mt_not_empty (#hsz:pos): merkle_tree #hsz -> GTot bool
val mt_not_empty (#hsz:pos): merkle_tree #hsz -> GTot bool
let mt_not_empty #hsz mt = MT?.j mt > 0
{ "file_name": "src/MerkleTree.New.High.fst", "git_rev": "7d7bdc20f2033171e279c176b26e84f9069d23c6", "git_url": "https://github.com/hacl-star/merkle-tree.git", "project_name": "merkle-tree" }
{ "end_col": 14, "end_line": 89, "start_col": 0, "start_line": 88 }
module MerkleTree.New.High open FStar.Ghost open FStar.Seq module S = FStar.Seq module U32 = FStar.UInt32 module U8 = FStar.UInt8 module MTS = MerkleTree.Spec #set-options "--z3rlimit 10 --max_fuel 0 --max_ifuel 0" type uint32_t = U32.t type uint8_t = U8.t type hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes{Seq.length b = hsz} type hashes (#hsz:pos) = S.seq (hash #hsz) type hashess (#hsz:pos) = S.seq (hashes #hsz) noextract let hash_init (#hsz:pos): hash #hsz = Seq.create hsz (Lib.IntTypes.u8 0) val sha256_compress: src1:hash #32 -> src2:hash #32 -> GTot (hash #32) let sha256_compress = MTS.sha256_compress /// Facts about sequences val seq_slice_equal_index: #a:Type -> s1:S.seq a -> s2:S.seq a -> i:nat -> j:nat{i <= j && j <= S.length s1 && j <= S.length s2} -> k:nat{i <= k && k < j} -> Lemma (requires S.equal (S.slice s1 i j) (S.slice s2 i j)) (ensures S.index s1 k == S.index s2 k) [SMTPat (S.equal (S.slice s1 i j) (S.slice s2 i j)); SMTPat (S.index s1 k)] let seq_slice_equal_index #a s1 s2 i j k = assert (S.index (S.slice s1 i j) (k - i) == S.index (S.slice s2 i j) (k - i)) private val seq_slice_more_equal: #a:Type -> s1:S.seq a -> s2:S.seq a -> n:nat -> m:nat{n <= m && m <= S.length s1 && m <= S.length s2} -> k:nat{n <= k} -> l:nat{k <= l && l <= m} -> Lemma (requires S.equal (S.slice s1 n m) (S.slice s2 n m)) (ensures S.equal (S.slice s1 k l) (S.slice s2 k l)) [SMTPat (S.equal (S.slice s1 n m) (S.slice s2 n m)); SMTPat (S.equal (S.slice s1 k l) (S.slice s2 k l))] private let seq_slice_more_equal #a s1 s2 n m k l = slice_slice s1 n m (k - n) (l - n); slice_slice s2 n m (k - n) (l - n) /// Facts about "2" val remainder_2_not_1_div: n:nat -> Lemma (requires n % 2 <> 1) (ensures n / 2 = (n + 1) / 2) let remainder_2_not_1_div n = () val remainder_2_1_div: n:nat -> Lemma (requires n % 2 = 1) (ensures n / 2 + 1 = (n + 1) / 2) let remainder_2_1_div n = () /// High-level Merkle tree data structure noeq type merkle_tree (#hsz:pos) = | MT: i:nat -> j:nat{i <= j && j < pow2 32} -> hs:hashess #hsz {S.length hs = 32} -> rhs_ok:bool -> rhs:hashes #hsz {S.length rhs = 32} -> // Rightmost hashes mroot:hash #hsz -> hash_fun:MTS.hash_fun_t #hsz -> merkle_tree #hsz val mt_not_full (#hsz:pos): merkle_tree #hsz -> GTot bool let mt_not_full #hsz mt = MT?.j mt < pow2 32 - 1 val mt_empty (#hsz:pos): merkle_tree #hsz -> GTot bool let mt_empty #hsz mt = MT?.j mt = 0
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "MerkleTree.Spec.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "MerkleTree.New.High.fst" }
[ { "abbrev": true, "full_module": "MerkleTree.Spec", "short_module": "MTS" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "MerkleTree.New", "short_module": null }, { "abbrev": false, "full_module": "MerkleTree.New", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
mt: MerkleTree.New.High.merkle_tree -> Prims.GTot Prims.bool
Prims.GTot
[ "sometrivial" ]
[]
[ "Prims.pos", "MerkleTree.New.High.merkle_tree", "Prims.op_GreaterThan", "MerkleTree.New.High.__proj__MT__item__j", "Prims.bool" ]
[]
false
false
false
false
false
let mt_not_empty #hsz mt =
MT?.j mt > 0
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_from_mont_zero
val lemma_from_mont_zero: a:S.felem -> Lemma (from_mont a == 0 <==> a == 0)
val lemma_from_mont_zero: a:S.felem -> Lemma (from_mont a == 0 <==> a == 0)
let lemma_from_mont_zero a = Spec.P256.Lemmas.prime_lemma (); Lib.NatMod.lemma_mul_mod_prime_zero #S.prime a fmont_R_inv
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 60, "end_line": 208, "start_col": 0, "start_line": 206 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime //--------------------------------------// // bn_mont_reduction is x * fmont_R_inv // //--------------------------------------// val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0) let lemma_prime_mont () = assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0) let bn_mont_reduction_lemma x n = lemma_prime_mont (); assert (SBM.bn_mont_pre n (u64 1)); let d, _ = SBML.eea_pow2_odd 256 (BD.bn_v n) in let res = SBM.bn_mont_reduction n (u64 1) x in assert_norm (S.prime * S.prime < S.prime * pow2 256); assert (BD.bn_v x < S.prime * pow2 256); SBM.bn_mont_reduction_lemma n (u64 1) x; assert (BD.bn_v res == SBML.mont_reduction 64 4 (BD.bn_v n) 1 (BD.bn_v x)); SBML.mont_reduction_lemma 64 4 (BD.bn_v n) 1 (BD.bn_v x); assert (BD.bn_v res == BD.bn_v x * d % S.prime); calc (==) { BD.bn_v x * d % S.prime; (==) { Math.Lemmas.lemma_mod_mul_distr_r (BD.bn_v x) d S.prime } BD.bn_v x * (d % S.prime) % S.prime; (==) { } BD.bn_v x * fmont_R_inv % S.prime; } //---------------------------
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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: Spec.P256.PointOps.felem -> FStar.Pervasives.Lemma (ensures Hacl.Spec.P256.Montgomery.from_mont a == 0 <==> a == 0)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.P256.PointOps.felem", "Lib.NatMod.lemma_mul_mod_prime_zero", "Spec.P256.PointOps.prime", "Hacl.Spec.P256.Montgomery.fmont_R_inv", "Prims.unit", "Spec.P256.Lemmas.prime_lemma" ]
[]
true
false
true
false
false
let lemma_from_mont_zero a =
Spec.P256.Lemmas.prime_lemma (); Lib.NatMod.lemma_mul_mod_prime_zero #S.prime a fmont_R_inv
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_to_from_mont_id
val lemma_to_from_mont_id: a:S.felem -> Lemma (from_mont (to_mont a) == a)
val lemma_to_from_mont_id: a:S.felem -> Lemma (from_mont (to_mont a) == a)
let lemma_to_from_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_to_from_mont_id_gen S.prime fmont_R fmont_R_inv a
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 57, "end_line": 213, "start_col": 0, "start_line": 211 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime //--------------------------------------// // bn_mont_reduction is x * fmont_R_inv // //--------------------------------------// val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0) let lemma_prime_mont () = assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0) let bn_mont_reduction_lemma x n = lemma_prime_mont (); assert (SBM.bn_mont_pre n (u64 1)); let d, _ = SBML.eea_pow2_odd 256 (BD.bn_v n) in let res = SBM.bn_mont_reduction n (u64 1) x in assert_norm (S.prime * S.prime < S.prime * pow2 256); assert (BD.bn_v x < S.prime * pow2 256); SBM.bn_mont_reduction_lemma n (u64 1) x; assert (BD.bn_v res == SBML.mont_reduction 64 4 (BD.bn_v n) 1 (BD.bn_v x)); SBML.mont_reduction_lemma 64 4 (BD.bn_v n) 1 (BD.bn_v x); assert (BD.bn_v res == BD.bn_v x * d % S.prime); calc (==) { BD.bn_v x * d % S.prime; (==) { Math.Lemmas.lemma_mod_mul_distr_r (BD.bn_v x) d S.prime } BD.bn_v x * (d % S.prime) % S.prime; (==) { } BD.bn_v x * fmont_R_inv % S.prime; } //--------------------------- let lemma_from_mont_zero a = Spec.P256.Lemmas.prime_lemma (); Lib.NatMod.lemma_mul_mod_prime_zero #S.prime a fmont_R_inv
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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: Spec.P256.PointOps.felem -> FStar.Pervasives.Lemma (ensures Hacl.Spec.P256.Montgomery.from_mont (Hacl.Spec.P256.Montgomery.to_mont a) == a)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.P256.PointOps.felem", "Hacl.Spec.P256.Montgomery.lemma_to_from_mont_id_gen", "Spec.P256.PointOps.prime", "Hacl.Spec.P256.Montgomery.fmont_R", "Hacl.Spec.P256.Montgomery.fmont_R_inv", "Prims.unit", "Hacl.Spec.P256.Montgomery.mul_fmont_R_and_R_inv_is_one" ]
[]
true
false
true
false
false
let lemma_to_from_mont_id a =
mul_fmont_R_and_R_inv_is_one (); lemma_to_from_mont_id_gen S.prime fmont_R fmont_R_inv a
false
MerkleTree.New.High.fst
MerkleTree.New.High.mt_empty
val mt_empty (#hsz:pos): merkle_tree #hsz -> GTot bool
val mt_empty (#hsz:pos): merkle_tree #hsz -> GTot bool
let mt_empty #hsz mt = MT?.j mt = 0
{ "file_name": "src/MerkleTree.New.High.fst", "git_rev": "7d7bdc20f2033171e279c176b26e84f9069d23c6", "git_url": "https://github.com/hacl-star/merkle-tree.git", "project_name": "merkle-tree" }
{ "end_col": 14, "end_line": 85, "start_col": 0, "start_line": 84 }
module MerkleTree.New.High open FStar.Ghost open FStar.Seq module S = FStar.Seq module U32 = FStar.UInt32 module U8 = FStar.UInt8 module MTS = MerkleTree.Spec #set-options "--z3rlimit 10 --max_fuel 0 --max_ifuel 0" type uint32_t = U32.t type uint8_t = U8.t type hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes{Seq.length b = hsz} type hashes (#hsz:pos) = S.seq (hash #hsz) type hashess (#hsz:pos) = S.seq (hashes #hsz) noextract let hash_init (#hsz:pos): hash #hsz = Seq.create hsz (Lib.IntTypes.u8 0) val sha256_compress: src1:hash #32 -> src2:hash #32 -> GTot (hash #32) let sha256_compress = MTS.sha256_compress /// Facts about sequences val seq_slice_equal_index: #a:Type -> s1:S.seq a -> s2:S.seq a -> i:nat -> j:nat{i <= j && j <= S.length s1 && j <= S.length s2} -> k:nat{i <= k && k < j} -> Lemma (requires S.equal (S.slice s1 i j) (S.slice s2 i j)) (ensures S.index s1 k == S.index s2 k) [SMTPat (S.equal (S.slice s1 i j) (S.slice s2 i j)); SMTPat (S.index s1 k)] let seq_slice_equal_index #a s1 s2 i j k = assert (S.index (S.slice s1 i j) (k - i) == S.index (S.slice s2 i j) (k - i)) private val seq_slice_more_equal: #a:Type -> s1:S.seq a -> s2:S.seq a -> n:nat -> m:nat{n <= m && m <= S.length s1 && m <= S.length s2} -> k:nat{n <= k} -> l:nat{k <= l && l <= m} -> Lemma (requires S.equal (S.slice s1 n m) (S.slice s2 n m)) (ensures S.equal (S.slice s1 k l) (S.slice s2 k l)) [SMTPat (S.equal (S.slice s1 n m) (S.slice s2 n m)); SMTPat (S.equal (S.slice s1 k l) (S.slice s2 k l))] private let seq_slice_more_equal #a s1 s2 n m k l = slice_slice s1 n m (k - n) (l - n); slice_slice s2 n m (k - n) (l - n) /// Facts about "2" val remainder_2_not_1_div: n:nat -> Lemma (requires n % 2 <> 1) (ensures n / 2 = (n + 1) / 2) let remainder_2_not_1_div n = () val remainder_2_1_div: n:nat -> Lemma (requires n % 2 = 1) (ensures n / 2 + 1 = (n + 1) / 2) let remainder_2_1_div n = () /// High-level Merkle tree data structure noeq type merkle_tree (#hsz:pos) = | MT: i:nat -> j:nat{i <= j && j < pow2 32} -> hs:hashess #hsz {S.length hs = 32} -> rhs_ok:bool -> rhs:hashes #hsz {S.length rhs = 32} -> // Rightmost hashes mroot:hash #hsz -> hash_fun:MTS.hash_fun_t #hsz -> merkle_tree #hsz val mt_not_full (#hsz:pos): merkle_tree #hsz -> GTot bool let mt_not_full #hsz mt = MT?.j mt < pow2 32 - 1
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "MerkleTree.Spec.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "MerkleTree.New.High.fst" }
[ { "abbrev": true, "full_module": "MerkleTree.Spec", "short_module": "MTS" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "MerkleTree.New", "short_module": null }, { "abbrev": false, "full_module": "MerkleTree.New", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
mt: MerkleTree.New.High.merkle_tree -> Prims.GTot Prims.bool
Prims.GTot
[ "sometrivial" ]
[]
[ "Prims.pos", "MerkleTree.New.High.merkle_tree", "Prims.op_Equality", "Prims.int", "MerkleTree.New.High.__proj__MT__item__j", "Prims.bool" ]
[]
false
false
false
false
false
let mt_empty #hsz mt =
MT?.j mt = 0
false
MerkleTree.New.High.fst
MerkleTree.New.High.mt_not_full
val mt_not_full (#hsz:pos): merkle_tree #hsz -> GTot bool
val mt_not_full (#hsz:pos): merkle_tree #hsz -> GTot bool
let mt_not_full #hsz mt = MT?.j mt < pow2 32 - 1
{ "file_name": "src/MerkleTree.New.High.fst", "git_rev": "7d7bdc20f2033171e279c176b26e84f9069d23c6", "git_url": "https://github.com/hacl-star/merkle-tree.git", "project_name": "merkle-tree" }
{ "end_col": 24, "end_line": 81, "start_col": 0, "start_line": 80 }
module MerkleTree.New.High open FStar.Ghost open FStar.Seq module S = FStar.Seq module U32 = FStar.UInt32 module U8 = FStar.UInt8 module MTS = MerkleTree.Spec #set-options "--z3rlimit 10 --max_fuel 0 --max_ifuel 0" type uint32_t = U32.t type uint8_t = U8.t type hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes{Seq.length b = hsz} type hashes (#hsz:pos) = S.seq (hash #hsz) type hashess (#hsz:pos) = S.seq (hashes #hsz) noextract let hash_init (#hsz:pos): hash #hsz = Seq.create hsz (Lib.IntTypes.u8 0) val sha256_compress: src1:hash #32 -> src2:hash #32 -> GTot (hash #32) let sha256_compress = MTS.sha256_compress /// Facts about sequences val seq_slice_equal_index: #a:Type -> s1:S.seq a -> s2:S.seq a -> i:nat -> j:nat{i <= j && j <= S.length s1 && j <= S.length s2} -> k:nat{i <= k && k < j} -> Lemma (requires S.equal (S.slice s1 i j) (S.slice s2 i j)) (ensures S.index s1 k == S.index s2 k) [SMTPat (S.equal (S.slice s1 i j) (S.slice s2 i j)); SMTPat (S.index s1 k)] let seq_slice_equal_index #a s1 s2 i j k = assert (S.index (S.slice s1 i j) (k - i) == S.index (S.slice s2 i j) (k - i)) private val seq_slice_more_equal: #a:Type -> s1:S.seq a -> s2:S.seq a -> n:nat -> m:nat{n <= m && m <= S.length s1 && m <= S.length s2} -> k:nat{n <= k} -> l:nat{k <= l && l <= m} -> Lemma (requires S.equal (S.slice s1 n m) (S.slice s2 n m)) (ensures S.equal (S.slice s1 k l) (S.slice s2 k l)) [SMTPat (S.equal (S.slice s1 n m) (S.slice s2 n m)); SMTPat (S.equal (S.slice s1 k l) (S.slice s2 k l))] private let seq_slice_more_equal #a s1 s2 n m k l = slice_slice s1 n m (k - n) (l - n); slice_slice s2 n m (k - n) (l - n) /// Facts about "2" val remainder_2_not_1_div: n:nat -> Lemma (requires n % 2 <> 1) (ensures n / 2 = (n + 1) / 2) let remainder_2_not_1_div n = () val remainder_2_1_div: n:nat -> Lemma (requires n % 2 = 1) (ensures n / 2 + 1 = (n + 1) / 2) let remainder_2_1_div n = () /// High-level Merkle tree data structure noeq type merkle_tree (#hsz:pos) = | MT: i:nat -> j:nat{i <= j && j < pow2 32} -> hs:hashess #hsz {S.length hs = 32} -> rhs_ok:bool -> rhs:hashes #hsz {S.length rhs = 32} -> // Rightmost hashes mroot:hash #hsz -> hash_fun:MTS.hash_fun_t #hsz -> merkle_tree #hsz
{ "checked_file": "/", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "MerkleTree.Spec.fst.checked", "Lib.IntTypes.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "MerkleTree.New.High.fst" }
[ { "abbrev": true, "full_module": "MerkleTree.Spec", "short_module": "MTS" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": false, "full_module": "MerkleTree.New", "short_module": null }, { "abbrev": false, "full_module": "MerkleTree.New", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
mt: MerkleTree.New.High.merkle_tree -> Prims.GTot Prims.bool
Prims.GTot
[ "sometrivial" ]
[]
[ "Prims.pos", "MerkleTree.New.High.merkle_tree", "Prims.op_LessThan", "MerkleTree.New.High.__proj__MT__item__j", "Prims.op_Subtraction", "Prims.pow2", "Prims.bool" ]
[]
false
false
false
false
false
let mt_not_full #hsz mt =
MT?.j mt < pow2 32 - 1
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_prime_mont
val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0)
val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0)
let lemma_prime_mont () = assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0)
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 43, "end_line": 180, "start_col": 0, "start_line": 177 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime //--------------------------------------// // bn_mont_reduction is x * fmont_R_inv // //--------------------------------------// val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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.P256.PointOps.prime % 2 = 1 /\ Spec.P256.PointOps.prime < Prims.pow2 256 /\ (1 + Spec.P256.PointOps.prime) % Prims.pow2 64 = 0)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Prims.op_Modulus", "Prims.op_Addition", "Spec.P256.PointOps.prime", "Prims.pow2", "Prims.op_LessThan" ]
[]
true
false
true
false
false
let lemma_prime_mont () =
assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0)
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.mul_fmont_R_and_R_inv_is_one
val mul_fmont_R_and_R_inv_is_one: unit -> Lemma (fmont_R * fmont_R_inv % S.prime = 1)
val mul_fmont_R_and_R_inv_is_one: unit -> Lemma (fmont_R * fmont_R_inv % S.prime = 1)
let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 56, "end_line": 168, "start_col": 0, "start_line": 164 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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 Hacl.Spec.P256.Montgomery.fmont_R * Hacl.Spec.P256.Montgomery.fmont_R_inv % Spec.P256.PointOps.prime = 1)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.unit", "Prims.int", "FStar.Math.Lemmas.lemma_mod_mul_distr_l", "Prims.pow2", "Spec.P256.PointOps.prime", "Prims._assert", "Prims.b2t", "Prims.op_Equality", "Prims.op_Modulus", "FStar.Mul.op_Star", "Hacl.Spec.Montgomery.Lemmas.mont_preconditions_d", "FStar.Pervasives.Native.tuple2", "Hacl.Spec.Montgomery.Lemmas.eea_pow2_odd" ]
[]
false
false
true
false
false
let mul_fmont_R_and_R_inv_is_one () =
let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.fmont_mul_lemma
val fmont_mul_lemma: a:S.felem -> b:S.felem -> Lemma (S.fmul (from_mont a) (from_mont b) = from_mont ((a * b * fmont_R_inv) % S.prime))
val fmont_mul_lemma: a:S.felem -> b:S.felem -> Lemma (S.fmul (from_mont a) (from_mont b) = from_mont ((a * b * fmont_R_inv) % S.prime))
let fmont_mul_lemma a b = mont_mul_lemma_gen S.prime fmont_R_inv a b
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 44, "end_line": 222, "start_col": 0, "start_line": 221 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime //--------------------------------------// // bn_mont_reduction is x * fmont_R_inv // //--------------------------------------// val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0) let lemma_prime_mont () = assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0) let bn_mont_reduction_lemma x n = lemma_prime_mont (); assert (SBM.bn_mont_pre n (u64 1)); let d, _ = SBML.eea_pow2_odd 256 (BD.bn_v n) in let res = SBM.bn_mont_reduction n (u64 1) x in assert_norm (S.prime * S.prime < S.prime * pow2 256); assert (BD.bn_v x < S.prime * pow2 256); SBM.bn_mont_reduction_lemma n (u64 1) x; assert (BD.bn_v res == SBML.mont_reduction 64 4 (BD.bn_v n) 1 (BD.bn_v x)); SBML.mont_reduction_lemma 64 4 (BD.bn_v n) 1 (BD.bn_v x); assert (BD.bn_v res == BD.bn_v x * d % S.prime); calc (==) { BD.bn_v x * d % S.prime; (==) { Math.Lemmas.lemma_mod_mul_distr_r (BD.bn_v x) d S.prime } BD.bn_v x * (d % S.prime) % S.prime; (==) { } BD.bn_v x * fmont_R_inv % S.prime; } //--------------------------- let lemma_from_mont_zero a = Spec.P256.Lemmas.prime_lemma (); Lib.NatMod.lemma_mul_mod_prime_zero #S.prime a fmont_R_inv let lemma_to_from_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_to_from_mont_id_gen S.prime fmont_R fmont_R_inv a let lemma_from_to_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_from_to_mont_id_gen S.prime fmont_R fmont_R_inv a
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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: Spec.P256.PointOps.felem -> b: Spec.P256.PointOps.felem -> FStar.Pervasives.Lemma (ensures Spec.P256.PointOps.fmul (Hacl.Spec.P256.Montgomery.from_mont a) (Hacl.Spec.P256.Montgomery.from_mont b) = Hacl.Spec.P256.Montgomery.from_mont ((a * b) * Hacl.Spec.P256.Montgomery.fmont_R_inv % Spec.P256.PointOps.prime))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.P256.PointOps.felem", "Hacl.Spec.P256.Montgomery.mont_mul_lemma_gen", "Spec.P256.PointOps.prime", "Hacl.Spec.P256.Montgomery.fmont_R_inv", "Prims.unit" ]
[]
true
false
true
false
false
let fmont_mul_lemma a b =
mont_mul_lemma_gen S.prime fmont_R_inv a b
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_mont_inv_gen
val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n)
val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n)
let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2)
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 30, "end_line": 136, "start_col": 0, "start_line": 118 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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
n: Prims.pos{1 < n} -> mont_R: Prims.pos -> mont_R_inv: Prims.nat{mont_R_inv < n} -> a: Prims.nat{a < n} -> FStar.Pervasives.Lemma (requires Lib.NatMod.pow_mod mont_R_inv (n - 2) == mont_R % n) (ensures Lib.NatMod.pow_mod (a * mont_R_inv % n) (n - 2) == Lib.NatMod.pow_mod a (n - 2) * mont_R % n )
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "Prims.b2t", "Prims.op_LessThan", "Prims.nat", "Lib.NatMod.lemma_pow_mod", "Prims.op_Subtraction", "Prims.unit", "FStar.Math.Lemmas.lemma_mod_mul_distr_l", "Lib.NatMod.pow", "FStar.Math.Lemmas.lemma_mod_mul_distr_r", "Prims._assert", "Prims.eq2", "Prims.int", "Lib.NatMod.pow_mod", "Prims.op_Modulus", "FStar.Mul.op_Star", "Lib.NatMod.lemma_pow_mul_base", "Lib.NatMod.lemma_pow_mod_base" ]
[]
true
false
true
false
false
let lemma_mont_inv_gen n mont_R mont_R_inv k =
M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; M.lemma_pow_mul_base k mont_R_inv (n - 2); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; M.lemma_pow_mod #n k (n - 2)
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.fmont_add_lemma
val fmont_add_lemma: a:S.felem -> b:S.felem -> Lemma (S.fadd (from_mont a) (from_mont b) = from_mont ((a + b) % S.prime))
val fmont_add_lemma: a:S.felem -> b:S.felem -> Lemma (S.fadd (from_mont a) (from_mont b) = from_mont ((a + b) % S.prime))
let fmont_add_lemma a b = mont_add_lemma_gen S.prime fmont_R_inv a b
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 44, "end_line": 226, "start_col": 0, "start_line": 225 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime //--------------------------------------// // bn_mont_reduction is x * fmont_R_inv // //--------------------------------------// val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0) let lemma_prime_mont () = assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0) let bn_mont_reduction_lemma x n = lemma_prime_mont (); assert (SBM.bn_mont_pre n (u64 1)); let d, _ = SBML.eea_pow2_odd 256 (BD.bn_v n) in let res = SBM.bn_mont_reduction n (u64 1) x in assert_norm (S.prime * S.prime < S.prime * pow2 256); assert (BD.bn_v x < S.prime * pow2 256); SBM.bn_mont_reduction_lemma n (u64 1) x; assert (BD.bn_v res == SBML.mont_reduction 64 4 (BD.bn_v n) 1 (BD.bn_v x)); SBML.mont_reduction_lemma 64 4 (BD.bn_v n) 1 (BD.bn_v x); assert (BD.bn_v res == BD.bn_v x * d % S.prime); calc (==) { BD.bn_v x * d % S.prime; (==) { Math.Lemmas.lemma_mod_mul_distr_r (BD.bn_v x) d S.prime } BD.bn_v x * (d % S.prime) % S.prime; (==) { } BD.bn_v x * fmont_R_inv % S.prime; } //--------------------------- let lemma_from_mont_zero a = Spec.P256.Lemmas.prime_lemma (); Lib.NatMod.lemma_mul_mod_prime_zero #S.prime a fmont_R_inv let lemma_to_from_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_to_from_mont_id_gen S.prime fmont_R fmont_R_inv a let lemma_from_to_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_from_to_mont_id_gen S.prime fmont_R fmont_R_inv a let fmont_mul_lemma a b = mont_mul_lemma_gen S.prime fmont_R_inv a b
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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: Spec.P256.PointOps.felem -> b: Spec.P256.PointOps.felem -> FStar.Pervasives.Lemma (ensures Spec.P256.PointOps.fadd (Hacl.Spec.P256.Montgomery.from_mont a) (Hacl.Spec.P256.Montgomery.from_mont b) = Hacl.Spec.P256.Montgomery.from_mont ((a + b) % Spec.P256.PointOps.prime))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.P256.PointOps.felem", "Hacl.Spec.P256.Montgomery.mont_add_lemma_gen", "Spec.P256.PointOps.prime", "Hacl.Spec.P256.Montgomery.fmont_R_inv", "Prims.unit" ]
[]
true
false
true
false
false
let fmont_add_lemma a b =
mont_add_lemma_gen S.prime fmont_R_inv a b
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.mont_sub_lemma_gen
val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n)
val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n)
let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; }
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 110, "start_col": 0, "start_line": 99 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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
n: Prims.pos -> mont_R_inv: Prims.nat -> a: Prims.nat -> b: Prims.nat -> FStar.Pervasives.Lemma (ensures (a * mont_R_inv % n - b * mont_R_inv % n) % n == ((a - b) % n) * mont_R_inv % n)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "Prims.nat", "FStar.Calc.calc_finish", "Prims.int", "Prims.eq2", "Prims.op_Modulus", "Prims.op_Subtraction", "FStar.Mul.op_Star", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Math.Lemmas.lemma_mod_sub_distr", "Prims.squash", "FStar.Math.Lemmas.lemma_mod_plus_distr_l", "Prims.op_Minus", "FStar.Math.Lemmas.distributivity_sub_left", "FStar.Math.Lemmas.lemma_mod_mul_distr_l" ]
[]
false
false
true
false
false
let mont_sub_lemma_gen n mont_R_inv a b =
calc ( == ) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; ( == ) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; ( == ) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; ( == ) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; ( == ) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } ((a - b) % n) * mont_R_inv % n; }
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_from_to_mont_id
val lemma_from_to_mont_id: a:S.felem -> Lemma (to_mont (from_mont a) == a)
val lemma_from_to_mont_id: a:S.felem -> Lemma (to_mont (from_mont a) == a)
let lemma_from_to_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_from_to_mont_id_gen S.prime fmont_R fmont_R_inv a
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 57, "end_line": 218, "start_col": 0, "start_line": 216 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime //--------------------------------------// // bn_mont_reduction is x * fmont_R_inv // //--------------------------------------// val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0) let lemma_prime_mont () = assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0) let bn_mont_reduction_lemma x n = lemma_prime_mont (); assert (SBM.bn_mont_pre n (u64 1)); let d, _ = SBML.eea_pow2_odd 256 (BD.bn_v n) in let res = SBM.bn_mont_reduction n (u64 1) x in assert_norm (S.prime * S.prime < S.prime * pow2 256); assert (BD.bn_v x < S.prime * pow2 256); SBM.bn_mont_reduction_lemma n (u64 1) x; assert (BD.bn_v res == SBML.mont_reduction 64 4 (BD.bn_v n) 1 (BD.bn_v x)); SBML.mont_reduction_lemma 64 4 (BD.bn_v n) 1 (BD.bn_v x); assert (BD.bn_v res == BD.bn_v x * d % S.prime); calc (==) { BD.bn_v x * d % S.prime; (==) { Math.Lemmas.lemma_mod_mul_distr_r (BD.bn_v x) d S.prime } BD.bn_v x * (d % S.prime) % S.prime; (==) { } BD.bn_v x * fmont_R_inv % S.prime; } //--------------------------- let lemma_from_mont_zero a = Spec.P256.Lemmas.prime_lemma (); Lib.NatMod.lemma_mul_mod_prime_zero #S.prime a fmont_R_inv let lemma_to_from_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_to_from_mont_id_gen S.prime fmont_R fmont_R_inv a
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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: Spec.P256.PointOps.felem -> FStar.Pervasives.Lemma (ensures Hacl.Spec.P256.Montgomery.to_mont (Hacl.Spec.P256.Montgomery.from_mont a) == a)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.P256.PointOps.felem", "Hacl.Spec.P256.Montgomery.lemma_from_to_mont_id_gen", "Spec.P256.PointOps.prime", "Hacl.Spec.P256.Montgomery.fmont_R", "Hacl.Spec.P256.Montgomery.fmont_R_inv", "Prims.unit", "Hacl.Spec.P256.Montgomery.mul_fmont_R_and_R_inv_is_one" ]
[]
true
false
true
false
false
let lemma_from_to_mont_id a =
mul_fmont_R_and_R_inv_is_one (); lemma_from_to_mont_id_gen S.prime fmont_R fmont_R_inv a
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.qmont_R_inv
val qmont_R_inv : pos
val qmont_R_inv : pos
let qmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.order in d % S.order
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 57, "end_line": 236, "start_col": 0, "start_line": 235 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime //--------------------------------------// // bn_mont_reduction is x * fmont_R_inv // //--------------------------------------// val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0) let lemma_prime_mont () = assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0) let bn_mont_reduction_lemma x n = lemma_prime_mont (); assert (SBM.bn_mont_pre n (u64 1)); let d, _ = SBML.eea_pow2_odd 256 (BD.bn_v n) in let res = SBM.bn_mont_reduction n (u64 1) x in assert_norm (S.prime * S.prime < S.prime * pow2 256); assert (BD.bn_v x < S.prime * pow2 256); SBM.bn_mont_reduction_lemma n (u64 1) x; assert (BD.bn_v res == SBML.mont_reduction 64 4 (BD.bn_v n) 1 (BD.bn_v x)); SBML.mont_reduction_lemma 64 4 (BD.bn_v n) 1 (BD.bn_v x); assert (BD.bn_v res == BD.bn_v x * d % S.prime); calc (==) { BD.bn_v x * d % S.prime; (==) { Math.Lemmas.lemma_mod_mul_distr_r (BD.bn_v x) d S.prime } BD.bn_v x * (d % S.prime) % S.prime; (==) { } BD.bn_v x * fmont_R_inv % S.prime; } //--------------------------- let lemma_from_mont_zero a = Spec.P256.Lemmas.prime_lemma (); Lib.NatMod.lemma_mul_mod_prime_zero #S.prime a fmont_R_inv let lemma_to_from_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_to_from_mont_id_gen S.prime fmont_R fmont_R_inv a let lemma_from_to_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_from_to_mont_id_gen S.prime fmont_R fmont_R_inv a let fmont_mul_lemma a b = mont_mul_lemma_gen S.prime fmont_R_inv a b let fmont_add_lemma a b = mont_add_lemma_gen S.prime fmont_R_inv a b let fmont_sub_lemma a b = mont_sub_lemma_gen S.prime fmont_R_inv a b /// Montgomery arithmetic for a scalar field
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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.pos
Prims.Tot
[ "total" ]
[]
[ "Prims.int", "Prims.op_Modulus", "Spec.P256.PointOps.order", "Prims.pos", "FStar.Pervasives.Native.tuple2", "Hacl.Spec.Montgomery.Lemmas.eea_pow2_odd" ]
[]
false
false
false
true
false
let qmont_R_inv =
let d, _ = SBML.eea_pow2_odd 256 S.order in d % S.order
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_order_mont
val lemma_order_mont: unit -> Lemma (S.order % 2 = 1 /\ S.order < pow2 256 /\ (1 + S.order * 0xccd1c8aaee00bc4f) % pow2 64 = 0)
val lemma_order_mont: unit -> Lemma (S.order % 2 = 1 /\ S.order < pow2 256 /\ (1 + S.order * 0xccd1c8aaee00bc4f) % pow2 64 = 0)
let lemma_order_mont () = assert_norm (S.order % 2 = 1); assert_norm (S.order < pow2 256); assert_norm ((1 + S.order * 0xccd1c8aaee00bc4f) % pow2 64 = 0)
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 64, "end_line": 256, "start_col": 0, "start_line": 253 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime //--------------------------------------// // bn_mont_reduction is x * fmont_R_inv // //--------------------------------------// val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0) let lemma_prime_mont () = assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0) let bn_mont_reduction_lemma x n = lemma_prime_mont (); assert (SBM.bn_mont_pre n (u64 1)); let d, _ = SBML.eea_pow2_odd 256 (BD.bn_v n) in let res = SBM.bn_mont_reduction n (u64 1) x in assert_norm (S.prime * S.prime < S.prime * pow2 256); assert (BD.bn_v x < S.prime * pow2 256); SBM.bn_mont_reduction_lemma n (u64 1) x; assert (BD.bn_v res == SBML.mont_reduction 64 4 (BD.bn_v n) 1 (BD.bn_v x)); SBML.mont_reduction_lemma 64 4 (BD.bn_v n) 1 (BD.bn_v x); assert (BD.bn_v res == BD.bn_v x * d % S.prime); calc (==) { BD.bn_v x * d % S.prime; (==) { Math.Lemmas.lemma_mod_mul_distr_r (BD.bn_v x) d S.prime } BD.bn_v x * (d % S.prime) % S.prime; (==) { } BD.bn_v x * fmont_R_inv % S.prime; } //--------------------------- let lemma_from_mont_zero a = Spec.P256.Lemmas.prime_lemma (); Lib.NatMod.lemma_mul_mod_prime_zero #S.prime a fmont_R_inv let lemma_to_from_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_to_from_mont_id_gen S.prime fmont_R fmont_R_inv a let lemma_from_to_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_from_to_mont_id_gen S.prime fmont_R fmont_R_inv a let fmont_mul_lemma a b = mont_mul_lemma_gen S.prime fmont_R_inv a b let fmont_add_lemma a b = mont_add_lemma_gen S.prime fmont_R_inv a b let fmont_sub_lemma a b = mont_sub_lemma_gen S.prime fmont_R_inv a b /// Montgomery arithmetic for a scalar field let qmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.order in d % S.order let mul_qmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.order in SBML.mont_preconditions_d 64 4 S.order; assert (d * pow2 256 % S.order = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.order; assert (d % S.order * pow2 256 % S.order = 1) //--------------------------------------// // bn_mont_reduction is x * qmont_R_inv // //--------------------------------------// val lemma_order_mont: unit -> Lemma (S.order % 2 = 1 /\ S.order < pow2 256 /\ (1 + S.order * 0xccd1c8aaee00bc4f) % pow2 64 = 0)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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.P256.PointOps.order % 2 = 1 /\ Spec.P256.PointOps.order < Prims.pow2 256 /\ (1 + Spec.P256.PointOps.order * 0xccd1c8aaee00bc4f) % Prims.pow2 64 = 0)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Prims.op_Modulus", "Prims.op_Addition", "FStar.Mul.op_Star", "Spec.P256.PointOps.order", "Prims.pow2", "Prims.op_LessThan" ]
[]
true
false
true
false
false
let lemma_order_mont () =
assert_norm (S.order % 2 = 1); assert_norm (S.order < pow2 256); assert_norm ((1 + S.order * 0xccd1c8aaee00bc4f) % pow2 64 = 0)
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.mont_cancel_lemma_gen
val mont_cancel_lemma_gen (n:pos) (mont_R mont_R_inv a b:nat) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R % n * b * mont_R_inv) % n = a * b % n)
val mont_cancel_lemma_gen (n:pos) (mont_R mont_R_inv a b:nat) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R % n * b * mont_R_inv) % n = a * b % n)
let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; }
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 157, "start_col": 0, "start_line": 139 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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
n: Prims.pos -> mont_R: Prims.nat -> mont_R_inv: Prims.nat -> a: Prims.nat -> b: Prims.nat -> FStar.Pervasives.Lemma (requires mont_R_inv * mont_R % n = 1) (ensures ((a * mont_R % n) * b) * mont_R_inv % n = a * b % n)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "Prims.nat", "FStar.Calc.calc_finish", "Prims.int", "Prims.eq2", "Prims.op_Modulus", "FStar.Mul.op_Star", "Prims.Cons", "FStar.Preorder.relation", "Prims.Nil", "Prims.unit", "FStar.Calc.calc_step", "FStar.Calc.calc_init", "FStar.Calc.calc_pack", "FStar.Math.Lemmas.paren_mul_right", "Prims.squash", "FStar.Math.Lemmas.lemma_mod_mul_distr_l", "FStar.Math.Lemmas.swap_mul", "FStar.Math.Lemmas.lemma_mod_mul_distr_r", "Prims._assert", "Prims.b2t", "Prims.op_Equality" ]
[]
false
false
true
false
false
let mont_cancel_lemma_gen n mont_R mont_R_inv a b =
calc ( == ) { (((a * mont_R % n) * b) * mont_R_inv) % n; ( == ) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } ((a * mont_R % n) * (b * mont_R_inv)) % n; ( == ) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } ((a * mont_R) * (b * mont_R_inv)) % n; ( == ) { (Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv)) } (a * ((b * mont_R_inv) * mont_R)) % n; ( == ) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; ( == ) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } ((a * b) * (mont_R_inv * mont_R)) % n; ( == ) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } ((a * b) * (mont_R_inv * mont_R % n)) % n; ( == ) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; }
false
Hacl.Spec.P256.Montgomery.fst
Hacl.Spec.P256.Montgomery.lemma_from_to_qmont_id
val lemma_from_to_qmont_id: a:S.qelem -> Lemma (to_qmont (from_qmont a) == a)
val lemma_from_to_qmont_id: a:S.qelem -> Lemma (to_qmont (from_qmont a) == a)
let lemma_from_to_qmont_id a = mul_qmont_R_and_R_inv_is_one (); Math.Lemmas.swap_mul qmont_R qmont_R_inv; lemma_from_to_mont_id_gen S.order qmont_R qmont_R_inv a
{ "file_name": "code/ecdsap256/Hacl.Spec.P256.Montgomery.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 57, "end_line": 291, "start_col": 0, "start_line": 288 }
module Hacl.Spec.P256.Montgomery open FStar.Mul open Lib.IntTypes module S = Spec.P256 module M = Lib.NatMod module BD = Hacl.Spec.Bignum.Definitions module SBM = Hacl.Spec.Bignum.Montgomery module SBML = Hacl.Spec.Montgomery.Lemmas #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" /// Montgomery arithmetic for a base field val lemma_abc_is_acb (a b c:nat) : Lemma (a * b * c = a * c * b) let lemma_abc_is_acb a b c = Math.Lemmas.paren_mul_right a b c; Math.Lemmas.swap_mul b c; Math.Lemmas.paren_mul_right a c b val lemma_mod_mul_assoc (n:pos) (a b c:nat) : Lemma ((a * b % n) * c % n == (a * (b * c % n)) % n) let lemma_mod_mul_assoc m a b c = calc (==) { (a * b % m) * c % m; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b) c m } (a * b) * c % m; (==) { Math.Lemmas.paren_mul_right a b c } a * (b * c) % m; (==) { Math.Lemmas.lemma_mod_mul_distr_r a (b * c) m } a * (b * c % m) % m; } val lemma_to_from_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R * mont_R_inv % n = 1) (ensures (a * mont_R % n) * mont_R_inv % n == a) let lemma_to_from_mont_id_gen n mont_R mont_R_inv a = lemma_mod_mul_assoc n a mont_R mont_R_inv; Math.Lemmas.modulo_lemma a n val lemma_from_to_mont_id_gen (n mont_R mont_R_inv:pos) (a:nat{a < n}) : Lemma (requires mont_R_inv * mont_R % n = 1) (ensures (a * mont_R_inv % n) * mont_R % n == a) let lemma_from_to_mont_id_gen n mont_R mont_R_inv a = lemma_to_from_mont_id_gen n mont_R_inv mont_R a val mont_mul_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma (((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n == ((a * b * mont_R_inv) % n) * mont_R_inv % n) let mont_mul_lemma_gen n mont_R_inv a b = calc (==) { ((a * mont_R_inv % n) * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R_inv) (b * mont_R_inv % n) n } (a * mont_R_inv * (b * mont_R_inv % n)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R_inv (b * mont_R_inv) } (a * (mont_R_inv * (b * mont_R_inv))) % n; (==) { Math.Lemmas.paren_mul_right mont_R_inv b mont_R_inv } (a * (mont_R_inv * b * mont_R_inv)) % n; (==) { Math.Lemmas.swap_mul mont_R_inv b } (a * (b * mont_R_inv * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a (b * mont_R_inv) mont_R_inv } (a * (b * mont_R_inv) * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right a b mont_R_inv } (a * b * mont_R_inv * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * b * mont_R_inv) mont_R_inv n } ((a * b * mont_R_inv) % n) * mont_R_inv % n; } val mont_add_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n + b * mont_R_inv % n) % n == (a + b) % n * mont_R_inv % n) let mont_add_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n + b * mont_R_inv % n) % n; (==) { Math.Lemmas.modulo_distributivity (a * mont_R_inv) (b * mont_R_inv) n } (a * mont_R_inv + b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_add_left a b mont_R_inv } (a + b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a + b) mont_R_inv n } (a + b) % n * mont_R_inv % n; } val mont_sub_lemma_gen (n:pos) (mont_R_inv a b: nat) : Lemma ((a * mont_R_inv % n - b * mont_R_inv % n) % n == (a - b) % n * mont_R_inv % n) let mont_sub_lemma_gen n mont_R_inv a b = calc (==) { (a * mont_R_inv % n - b * mont_R_inv % n) % n; (==) { Math.Lemmas.lemma_mod_sub_distr (a * mont_R_inv % n) (b * mont_R_inv) n } (a * mont_R_inv % n - b * mont_R_inv) % n; (==) { Math.Lemmas.lemma_mod_plus_distr_l (a * mont_R_inv) (- b * mont_R_inv) n } (a * mont_R_inv - b * mont_R_inv) % n; (==) { Math.Lemmas.distributivity_sub_left a b mont_R_inv } (a - b) * mont_R_inv % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a - b) mont_R_inv n } (a - b) % n * mont_R_inv % n; } val lemma_mont_inv_gen (n:pos{1 < n}) (mont_R:pos) (mont_R_inv:nat{mont_R_inv < n}) (a:nat{a < n}) : Lemma (requires M.pow_mod #n mont_R_inv (n - 2) == mont_R % n) (ensures M.pow_mod #n (a * mont_R_inv % n) (n - 2) == M.pow_mod #n a (n - 2) * mont_R % n) let lemma_mont_inv_gen n mont_R mont_R_inv k = M.lemma_pow_mod #n (k * mont_R_inv % n) (n - 2); // assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == // M.pow (k * mont_R_inv % n) (n - 2) % n); M.lemma_pow_mod_base (k * mont_R_inv) (n - 2) n; // == M.pow (k * mont_R_inv) (n - 2) % n M.lemma_pow_mul_base k mont_R_inv (n - 2); // == M.pow k (n - 2) * M.pow mont_R_inv (n - 2) % n Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) (M.pow mont_R_inv (n - 2)) n; // == M.pow k (n - 2) * (M.pow mont_R_inv (n - 2) % n) % n M.lemma_pow_mod #n mont_R_inv (n - 2); assert (M.pow_mod #n (k * mont_R_inv % n) (n - 2) == M.pow k (n - 2) * (mont_R % n) % n); Math.Lemmas.lemma_mod_mul_distr_r (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) * mont_R % n Math.Lemmas.lemma_mod_mul_distr_l (M.pow k (n - 2)) mont_R n; // == M.pow k (n - 2) % n * mont_R % n M.lemma_pow_mod #n k (n - 2) let mont_cancel_lemma_gen n mont_R mont_R_inv a b = calc (==) { (a * mont_R % n * b * mont_R_inv) % n; (==) { Math.Lemmas.paren_mul_right (a * mont_R % n) b mont_R_inv } (a * mont_R % n * (b * mont_R_inv)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_l (a * mont_R) (b * mont_R_inv) n } (a * mont_R * (b * mont_R_inv)) % n; (==) { Math.Lemmas.paren_mul_right a mont_R (b * mont_R_inv); Math.Lemmas.swap_mul mont_R (b * mont_R_inv) } (a * (b * mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.paren_mul_right b mont_R_inv mont_R } (a * (b * (mont_R_inv * mont_R))) % n; (==) { Math.Lemmas.paren_mul_right a b (mont_R_inv * mont_R) } (a * b * (mont_R_inv * mont_R)) % n; (==) { Math.Lemmas.lemma_mod_mul_distr_r (a * b) (mont_R_inv * mont_R) n } (a * b * (mont_R_inv * mont_R % n)) % n; (==) { assert (mont_R_inv * mont_R % n = 1) } (a * b) % n; } let fmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.prime in d % S.prime let mul_fmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.prime in SBML.mont_preconditions_d 64 4 S.prime; assert (d * pow2 256 % S.prime = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.prime //--------------------------------------// // bn_mont_reduction is x * fmont_R_inv // //--------------------------------------// val lemma_prime_mont: unit -> Lemma (S.prime % 2 = 1 /\ S.prime < pow2 256 /\ (1 + S.prime) % pow2 64 = 0) let lemma_prime_mont () = assert_norm (S.prime % 2 = 1); assert_norm (S.prime < pow2 256); assert_norm ((1 + S.prime) % pow2 64 = 0) let bn_mont_reduction_lemma x n = lemma_prime_mont (); assert (SBM.bn_mont_pre n (u64 1)); let d, _ = SBML.eea_pow2_odd 256 (BD.bn_v n) in let res = SBM.bn_mont_reduction n (u64 1) x in assert_norm (S.prime * S.prime < S.prime * pow2 256); assert (BD.bn_v x < S.prime * pow2 256); SBM.bn_mont_reduction_lemma n (u64 1) x; assert (BD.bn_v res == SBML.mont_reduction 64 4 (BD.bn_v n) 1 (BD.bn_v x)); SBML.mont_reduction_lemma 64 4 (BD.bn_v n) 1 (BD.bn_v x); assert (BD.bn_v res == BD.bn_v x * d % S.prime); calc (==) { BD.bn_v x * d % S.prime; (==) { Math.Lemmas.lemma_mod_mul_distr_r (BD.bn_v x) d S.prime } BD.bn_v x * (d % S.prime) % S.prime; (==) { } BD.bn_v x * fmont_R_inv % S.prime; } //--------------------------- let lemma_from_mont_zero a = Spec.P256.Lemmas.prime_lemma (); Lib.NatMod.lemma_mul_mod_prime_zero #S.prime a fmont_R_inv let lemma_to_from_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_to_from_mont_id_gen S.prime fmont_R fmont_R_inv a let lemma_from_to_mont_id a = mul_fmont_R_and_R_inv_is_one (); lemma_from_to_mont_id_gen S.prime fmont_R fmont_R_inv a let fmont_mul_lemma a b = mont_mul_lemma_gen S.prime fmont_R_inv a b let fmont_add_lemma a b = mont_add_lemma_gen S.prime fmont_R_inv a b let fmont_sub_lemma a b = mont_sub_lemma_gen S.prime fmont_R_inv a b /// Montgomery arithmetic for a scalar field let qmont_R_inv = let d, _ = SBML.eea_pow2_odd 256 S.order in d % S.order let mul_qmont_R_and_R_inv_is_one () = let d, k = SBML.eea_pow2_odd 256 S.order in SBML.mont_preconditions_d 64 4 S.order; assert (d * pow2 256 % S.order = 1); Math.Lemmas.lemma_mod_mul_distr_l d (pow2 256) S.order; assert (d % S.order * pow2 256 % S.order = 1) //--------------------------------------// // bn_mont_reduction is x * qmont_R_inv // //--------------------------------------// val lemma_order_mont: unit -> Lemma (S.order % 2 = 1 /\ S.order < pow2 256 /\ (1 + S.order * 0xccd1c8aaee00bc4f) % pow2 64 = 0) let lemma_order_mont () = assert_norm (S.order % 2 = 1); assert_norm (S.order < pow2 256); assert_norm ((1 + S.order * 0xccd1c8aaee00bc4f) % pow2 64 = 0) let bn_qmont_reduction_lemma x n = let k0 = 0xccd1c8aaee00bc4f in lemma_order_mont (); assert (SBM.bn_mont_pre n (u64 k0)); let d, _ = SBML.eea_pow2_odd 256 (BD.bn_v n) in let res = SBM.bn_mont_reduction n (u64 k0) x in assert_norm (S.order * S.order < S.order * pow2 256); assert (BD.bn_v x < S.order * pow2 256); SBM.bn_mont_reduction_lemma n (u64 k0) x; assert (BD.bn_v res == SBML.mont_reduction 64 4 (BD.bn_v n) k0 (BD.bn_v x)); SBML.mont_reduction_lemma 64 4 (BD.bn_v n) k0 (BD.bn_v x); assert (BD.bn_v res == BD.bn_v x * d % S.order); calc (==) { (BD.bn_v x) * d % S.order; (==) { Math.Lemmas.lemma_mod_mul_distr_r (BD.bn_v x) d S.order } (BD.bn_v x) * (d % S.order) % S.order; (==) { } (BD.bn_v x) * qmont_R_inv % S.order; } //-------------------------- let lemma_to_from_qmont_id a = mul_qmont_R_and_R_inv_is_one (); lemma_to_from_mont_id_gen S.order qmont_R qmont_R_inv a
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "prims.fst.checked", "Lib.NatMod.fsti.checked", "Lib.IntTypes.fsti.checked", "Hacl.Spec.Montgomery.Lemmas.fst.checked", "Hacl.Spec.Bignum.Montgomery.fsti.checked", "Hacl.Spec.Bignum.Definitions.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Calc.fsti.checked" ], "interface_file": true, "source_file": "Hacl.Spec.P256.Montgomery.fst" }
[ { "abbrev": true, "full_module": "Hacl.Spec.Montgomery.Lemmas", "short_module": "SBML" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Montgomery", "short_module": "SBM" }, { "abbrev": true, "full_module": "Hacl.Spec.Bignum.Definitions", "short_module": "BD" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "Lib.NatMod", "short_module": "M" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Spec.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: Spec.P256.PointOps.qelem -> FStar.Pervasives.Lemma (ensures Hacl.Spec.P256.Montgomery.to_qmont (Hacl.Spec.P256.Montgomery.from_qmont a) == a)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Spec.P256.PointOps.qelem", "Hacl.Spec.P256.Montgomery.lemma_from_to_mont_id_gen", "Spec.P256.PointOps.order", "Hacl.Spec.P256.Montgomery.qmont_R", "Hacl.Spec.P256.Montgomery.qmont_R_inv", "Prims.unit", "FStar.Math.Lemmas.swap_mul", "Hacl.Spec.P256.Montgomery.mul_qmont_R_and_R_inv_is_one" ]
[]
true
false
true
false
false
let lemma_from_to_qmont_id a =
mul_qmont_R_and_R_inv_is_one (); Math.Lemmas.swap_mul qmont_R qmont_R_inv; lemma_from_to_mont_id_gen S.order qmont_R qmont_R_inv a
false